Home > other >  How to convert ASP.NET webhost builder code to .NET 6 / avoid ASP0009 warning/error?
How to convert ASP.NET webhost builder code to .NET 6 / avoid ASP0009 warning/error?

Time:01-03

How can I create a WebApplicationBuilder instance without using Configure() which is deprecated in .NET6/7?

Compiler says: [ASP0009] Configure cannot be used with WebApplicationBuilder.WebHost

My current code snippet:

var builder = WebApplication.CreateBuilder();

builder.WebHost.Configure(app =>
    {
        app
            .UseRouting()
            .UseAuthentication()
            .UseEndpoints(_ => _.MapDefaultControllerRoute()
            );
    }
);
return builder;

I need a "builder" instance because after this code more configuration gets added before .Build() can be called.

I could not find a replacement especially for .UseEndPoints(...).

CodePudding user response:

For minimal APIs, you need to configure middlewares and routes on the WebApplication itself, such as:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(_ => _.MapDefaultControllerRoute());

app.Run();

CodePudding user response:

You should be able to move everything to the "root":

var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.MapDefaultControllerRoute(); 

Or with chaining:

var app = builder.Build();
app.UseRouting()
   .UseAuthentication(); // can chain this calls cause they use IApplicationBuilder

app.MapDefaultControllerRoute(); // no chaining here, requires IEndpointRouteBuilder
  • Related