How to use the Developer Exception Page in ASP.NET Core MVC

The Developer Exception Web site in ASP.Internet Main provides in-depth information about exceptions. Since builders need to have in-depth information about exceptions, and conclusion customers don’t, the ASP.Internet Main templates empower the Developer Exception Web site only when an software is operating in the Development setting.

This posting talks about the Developer Exception Web site Middleware in ASP.Internet Main MVC and how we can cope with exceptions in the two improvement and creation environments in ASP.Internet Main MVC apps.

To work with the code examples presented in this posting, you must have Visual Studio 2019 set up in your method. If you don’t presently have a duplicate, you can download Visual Studio 2019 right here.

Make an ASP.Internet Main MVC job in Visual Studio 2019

1st off, let’s generate an ASP.Internet Main job in Visual Studio 2019. Pursuing these actions will generate a new ASP.Internet Main MVC 5 job in Visual Studio 2019.

  1. Launch the Visual Studio IDE.
  2. Click on on “Create new job.”
  3. In the “Create new project” window, pick “ASP.Internet Main Website App (Model-See-Controller)” from the list of templates displayed.
  4. Click on Subsequent.
  5. In the “Configure your new project” window, specify the identify and location for the new job.
  6. Optionally look at the “Place answer and job in the exact directory” look at box, relying on your tastes.
  7. Click on Subsequent.
  8. In the “Additional Information” window revealed upcoming, pick .Internet 5. as the concentrate on framework from the fall-down list at the leading. Go away the “Authentication Type” as “None” (default).
  9. Assure that the look at bins “Enable Docker,” “Configure for HTTPS,” and “Enable Razor runtime compilation” are unchecked as we won’t be utilizing any of all those features right here.
  10. Click on Make.

A new ASP.Internet Main MVC job will be developed. We’ll use this job to work with the Developer Exception Web site middleware in the subsequent sections of this posting.

Subsequent abide by the actions outlined down below to generate extra controller(s) in your job:

  1. Correct-click on on the Controllers answer folder.
  2. Choose Include -> Controller.
  3. In the “Add New Scaffolded Item” dialog, pick API as the template (by default MVC would be picked).
  4. Choose the product “API Controller with read/compose actions.”
  5. Click on Include.
  6. In the “Add New Item” dialog revealed upcoming, specify a identify for your new controller.
  7. Click on Include.

Dealing with exceptions in ASP.Internet Main MVC

Take into account the adhering to piece of code that throws an exception in the Configure strategy of the startup course:

general public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 
      app.UseStaticFiles()
      app.UseRouting()
      app.UseAuthorization()
      app.UseEndpoints(endpoints = >
            endpoints.MapControllerRoute(
            identify: "default", sample:
"controller=Residence/motion=Index/id?")
      )
      toss new Exception("An error occurred while processing the ask for")

When you run the above software, an exception will be thrown with the error messages revealed in Determine one down below.

developer exception page 01 IDG

Determine one.

As you can see in Determine one, the error messages displayed right here are neither in-depth nor developer-helpful. A much better selection is to use the Developer Exception Web site.

Exception dealing with in the Development setting

The Developer Exception Web site middleware provides developer-helpful error messages when an exception is thrown in the software. This information allows you to trace the problems that occur when the software is operating in the Development setting.

The Developer Exception Web site groups this in-depth information into 5 tabbed sections — Stack, Question, Cookies, Headers, and Routing — as revealed in Determine 2 down below.

developer exception page 02 IDG

Determine 2.

When you generate a new ASP.Internet Main MVC job in Visual Studio, the ASP.Internet Main template generates the adhering to code in the Configure strategy of the Startup course.

general public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 
      if (env.IsDevelopment())
            app.UseDeveloperExceptionPage()
     
      else
            app.UseExceptionHandler("/Residence/Error")
     
      app.UseStaticFiles()
      app.UseRouting()
      app.UseAuthorization()
      app.UseEndpoints(endpoints = >
            endpoints.MapControllerRoute(
            identify: "default", sample:
"controller=Residence/motion=Index/id?")
      )

As the previous code indicates, the Developer Exception Web site will be enabled only when the software is operating in the Development setting. The Developer Exception Web site is not enabled when the software is operating in the Production setting. The natural way, the purpose is to prevent displaying exception messages publicly.

Take note that the UseDeveloperExceptionPage extension strategy is referred to as at the beginning of the pipeline, just before any other middleware is referred to as. This is carried out to make certain that exceptions in the adhering to middleware will be caught.

Exception dealing with in the Production setting

For dealing with exceptions in the Production setting, you must choose benefit of the UseExceptionHandler extension strategy. The adhering to code listing illustrates how the UseExceptionHandler extension strategy can be referred to as in the Configure strategy of the Startup course.

general public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 
      if (env.IsDevelopment())
            app.UseDeveloperExceptionPage()
     
      else
            app.UseExceptionHandler("/Residence/Error")
     
      app.UseStaticFiles()
      app.UseRouting()
      app.UseAuthorization()
      app.UseEndpoints(endpoints = >
            endpoints.MapControllerRoute(
            identify: "default", sample:
            "controller=Residence/motion=Index/id?")
      )

The UseExceptionHandler extension strategy can be utilized to configure custom error dealing with routes. Refer to the adhering to code snippet.

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 
      if (env.IsDevelopment())
            app.UseDeveloperExceptionPage(developerExceptionPageOptions)
     
      else
            app.UseExceptionHandler("/Residence/Error")
     
      //Other code taken out for brevity

When an error occurs in the ASP.Internet Main MVC software, the regulate will be redirected to /house/error.

Take note you could use a lambda expression in the UseExceptionHandler extension strategy in lieu of utilizing a custom exception handler web site. The adhering to code snippet illustrates how this can be achieved.

app.UseExceptionHandler(errorApp = >
      errorApp.Operate(async context = >
            context.Reaction.StatusCode = (int)
HttpStatusCode.InternalServerError
            context.Reaction.ContentType = "textual content/html"
            await context.Reaction.WriteAsync("lang="en">rn")
            await context.Reaction.WriteAsync("An error occurred...

rn")
            await context.Reaction.WriteAsync("rn")
            await context.Reaction.WriteAsync(new string(' ', 512))
      )
)

The Developer Exception Web site is out there by including the Developer Exception Web site middleware to the ASP.Internet Main MVC ask for processing pipeline. The default template in ASP.Internet Main MVC allows this middleware for the Development setting only. For Production, you can choose benefit of the UseExceptionHandler extension strategy to configure custom error dealing with.

Copyright © 2021 IDG Communications, Inc.

Rosa G. Rose

Next Post

What is the Rust language? Safe, fast, and easy software development

Mon Oct 11 , 2021
Rapid, harmless, easy to write—pick two. That’s been the state of software program advancement for a excellent very long time now. Languages that emphasize advantage and protection have a tendency to be slow (like Python). Languages that emphasize effectiveness have a tendency to be challenging to get the job done […]