current position:Home>Trial of new features of. Net 6 | no need to configure developer exception page

Trial of new features of. Net 6 | no need to configure developer exception page

2022-05-07 20:59:15Jade dust


stay .NET 6 Before , We need to be in “Startup.cs” Manually configure the developer exception page in the file :

if (env.IsDevelopment()){    app.UseDeveloperExceptionPage();    app.UseSwagger();    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication5 v1"));}

If the current environment variable ASPNETCORE_ENVIRONMENT The value of is “Development”, The details of the unhandled request exception are displayed :

.NET 6 Try out new features | There is no need to configure the developer exception page

stay .NET 6 in , We found that , No explicit call is required UseDeveloperExceptionPage, The developer exception page also works .

// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){    app.UseSwagger();    app.UseSwaggerUI();}

Why? ?

Principle Exploration

stay in , We found the following code :

private void ConfigureApplication(WebHostBuilderContext context, IApplicationBuilder app){    ......    if (context.HostingEnvironment.IsDevelopment())    {        app.UseDeveloperExceptionPage();    }        ......}internal WebApplicationBuilder(WebApplicationOptions options, Action<IHostBuilder>? configureDefaults = null){    ......    _bootstrapHostBuilder.ConfigureWebHostDefaults(webHostBuilder =>    {            // Runs inline.            webHostBuilder.Configure(ConfigureApplication);            // Attempt to set the application name from options            options.ApplyApplicationName(webHostBuilder);    });    ......}

It turns out that the default processing is carried out in the constructor , That is to say API Implemented in the first sentence of code ;

var builder = WebApplication.CreateBuilder(args);// Microsoft.AspNetCore.Builder.WebApplicationpublic static WebApplicationBuilder CreateBuilder(string[] args){ return new WebApplicationBuilder(new WebApplicationOptions {  Args = args });}

that .NET 6 What else did you do for us by default ?

Other default actions

Keep looking at ConfigureApplication Realization , You can find .NET 6 There are also other default processing , such as :

  • UseRouting
// If this is set, someone called UseRouting() when a global route builder was already setif (!_builtApplication.Properties.TryGetValue(EndpointRouteBuilderKey, out var localRouteBuilder)){    app.UseRouting();}else{    // UseEndpoints will be looking for the RouteBuilder so make sure it's set    app.Properties[EndpointRouteBuilderKey] = localRouteBuilder;}

And in the .NET 5 Must be configured manually in , Otherwise, the report will be wrong :

.NET 6 Try out new features | There is no need to configure the developer exception page

  • UseEndpoints
if (_builtApplication.DataSources.Count > 0){    // We don't know if user code called UseEndpoints(), so we will call it just in case, UseEndpoints() will ignore duplicate DataSources    app.UseEndpoints(_ => { });}

And in the .NET 5 You have to be in the UseEndpoints Method MapXXX Method

app.UseEndpoints(endpoints =>{    endpoints.MapGet("/", async context =>    {        await context.Response.WriteAsync("Hello My IO!");    });});


Precisely because .NET 6 The interior has done a lot of work for us , To minimize Web API Make it possible :

var builder = WebApplication.CreateBuilder(args);var app = builder.Build();app.MapGet("/", () => "Hello World!");app.Run();

Reduce the amount of initial code , This is undoubtedly more friendly for beginners !

copyright notice
author[Jade dust],Please bring the original link to reprint, thank you.

Random recommended