ASP.NET Core method in app.UseDeveloperExceptionPage and what is the use app.UseExceptionHandler

After a new ASP.NET Core project, project method in Configure Startup class by default add a call, app.UseDeveloperExceptionPage and app.UseExceptionHandler two methods, as follows:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
            
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

In fact app.UseDeveloperExceptionPage method is to tell ASP.NET Core, when the code exception error occurs, an exception error message is displayed on the page in a browser, that is, we in the development of the code, often see exception error page:

 

But we can see app.UseDeveloperExceptionPage method, is written if (env.IsDevelopment ()) the conditions inside, so when only in the development environment, ASP.NET Core exception error will be displayed above the page, and in the under other circumstances (such as the ASP.NET Core project release in which the production environment), will perform app.UseExceptionHandler method, which is passed a URL address, in this case we pass is the "/ Home / Error":

app.UseExceptionHandler("/Home/Error");

That in non-development environment, ASP.NET Core projects have code exception occurs, the page content ASP.NET Core will "/ Home / Error" This URL address is presented to the client browser, and "/ Home / error "is a custom MVC view page, you can define any content you want to display, where we only display an error message" background code error! ", the following is a view file" Error.cshtml "content:

 

Guess you like

Origin www.cnblogs.com/OpenCoder/p/11459985.html