How do I fix the Blazor app example given by Bing Chat?

Rod Falanga 566 Reputation points
2024-08-10T13:42:19.36+00:00

I'm working on a .NET 8 project to learn how to use minimal APIs, in the same project, that includes Blazor components. I don't know how to consume the minimal API in the project, so I asked Bing Copilot how to do that. In part, this is what it gave me that I should put into the Program.cs file:

builder.Services.AddRazorComponents();
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

However, Visual Studio 2022 complains about builder.HostEnvironment.BaseAddress. It says:

CS1061 'WebApplicationBuilder' does not contain a definition for 'HostEnvironment' and no accessible extension method 'HostEnvironment' accepting a first argument of type 'WebApplicationBuilder' could be found (are you missing a using directive or an assembly reference?

My guess is that section of code was valid in an older version of .NET but is no longer correct. I've tried rephrasing the question multiple times, but Bing Copilot continues to give me that code. So, my question is what should I use?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,584 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 28,371 Reputation points
    2024-08-10T14:24:41.22+00:00

    The code you've shared is for Blazor Client Side. Replace the code with the following to make HTTP calls from the server.

    builder.Services.AddHttpClient();
    
    

    That brings up the question why are you adding minimal API?

    I think your general design approach is a little shaky. Can you clarify the general design goals?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.