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

The Developer Exception Page in ASP.NET Core provides detailed information almost qualifications. Because developers need detailed information almost qualifications and end users dont the ASP.NET Core templates empower the Developer Exception Page only when an application is running in the Development environment.

This article talks almost the Developer Exception Page Middleware in ASP.NET Core MVC and how we can feel qualifications in both outgrowth and origination environments in ASP.NET Core MVC applications.

[ Also on InfoWorld: Whats new in Microsoft .NET 6 ]

To work with the code examples granted in this article you should have Visual Studio 2019 installed in your order. If you dont already have a copy you can download Visual Studio 2019 here.

Create an ASP.NET Core MVC project in Visual Studio 2019

First off lets form an ASP.NET Core project in Visual Studio 2019. Following these steps will form a new ASP.NET Core MVC 5 project in Visual Studio 2019.

  1. Launch the Visual Studio IDE.
  2. Click on ’Create new project.’
  3. In the ’Create new project’ window select ’ASP.NET Core Web App (Model-View-Controller)’ from the list of templates displayed.
  4. Click Next.
  5. In the ’Conaspect your new project’ window particularize the name and location for the new project.
  6. Optionally check the ’Place solution and project in the same directory’ check box depending on your preferences.
  7. Click Next.
  8. In the ’Additional Information’ window shown next select .NET 5.0 as the target framework from the drop-down list at the top. Leave the ’Authentication Type’ as ’None’ (lapse).
  9. Ensure that the check boxes ’Enable Docker’ ’Conaspect for HTTPS’ and ’Enable Razor runtime collation’ are unchecked as we wont be using any of those features here.
  10. Click Create.

A new ASP.NET Core MVC project will be formd. Well use this project to work with the Developer Exception Page middleware in the posterior sections of this article.

Next pursue the steps outlined under to form additional controller(s) in your project:

  1. Right-click on the Controllers solution folder.
  2. Select Add -gt; Controller.
  3. In the ’Add New Scaffolded Item’ dialog select API as the template (by lapse MVC would be selected).
  4. Select the item ’API Controller with read/write actions.’
  5. Click Add.
  6. In the ’Add New Item’ dialog shown next particularize a name for your new controller.
  7. Click Add.

Handling qualifications in ASP.NET Core MVC

Consider the pursueing piece of code that flings an qualification in the Conaspect order of the startup class:

open void Conaspect(IApplicationBuilder app IWebHostEnvironment env) {
      app.UseStaticFiles();
      app.UseRouting();
      app.UseAuthorization();
      app.UseEndpoints(endpoints = gt;{
            endpoints.MapControllerRoute(
            name: "lapse" model:
"{controller=Home}/{action=Index}/{id?}");
      });
      fling new Exception("An fault befallred while processing the request");
}

When you run the over application an qualification will be flingn with the fault messages shown in Figure 1 under.


IDG

Figure 1.

As you can see in Figure 1 the fault messages displayed here are neither detailed nor developer-friendly. A better discretion is to use the Developer Exception Page.

Exception handling in the Development environment

The Developer Exception Page middleware provides developer-friendly fault messages when an qualification is flingn in the application. This information helps you to explore the faults that befall when the application is running in the Development environment.

The Developer Exception Page groups this detailed information into five tabbed sections — Stack Query Cookies Headers and Routing — as shown in Figure 2 under.


IDG

Figure 2.

When you form a new ASP.NET Core MVC project in Visual Studio the ASP.NET Core template generates the pursueing code in the Conaspect order of the Startup class.

open void Conaspect(IApplicationBuilder app IWebHostEnvironment env) {
      if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
      }
      else {
            app.UseExceptionHandler("/Home/Error");
      }
      app.UseStaticFiles();
      app.UseRouting();
      app.UseAuthorization();
      app.UseEndpoints(endpoints = gt;{
            endpoints.MapControllerRoute(
            name: "lapse" model:
"{controller=Home}/{action=Index}/{id?}");
      });
}

As the precedent code indicates the Developer Exception Page will be empowerd only when the application is running in the Development environment. The Developer Exception Page is not empowerd when the application is running in the Production environment. Naturally the purpose is to quit displaying qualification messages openly.

Note that the UseDeveloperExceptionPage extension order is named at the commencement of the pipeline precedently any other middleware is named. This is done to fix that qualifications in the pursueing middleware will be caught.

[ Keep up with the latest outgrowths in Microsoft .NET and software outgrowth. Subscribe to the InfoWorld First Look newsletter ]

Exception handling in the Production environment

For handling qualifications in the Production environment you should take gain of the UseExceptionHandler extension order. The pursueing code listing illustrates how the UseExceptionHandler extension order can be named in the Conaspect order of the Startup class.

open void Conaspect(IApplicationBuilder app IWebHostEnvironment env) {
      if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
      }
      else {
            app.UseExceptionHandler("/Home/Error");
      }
      app.UseStaticFiles();
      app.UseRouting();
      app.UseAuthorization();
      app.UseEndpoints(endpoints = gt;{
            endpoints.MapControllerRoute(
            name: "lapse" model:
            "{controller=Home}/{action=Index}/{id?}");
      });
}

The UseExceptionHandler extension order can be used to conaspect manner fault handling routes. Refer to the pursueing code snippet.

 open void Conaspect(IApplicationBuilder app IWebHostEnvironment env) {
      if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage(developerExceptionPageOptions);
      }
      else {
            app.UseExceptionHandler("/Home/Error");
      }
      //Other code removed for shortness
}

When an fault befalls in the ASP.NET Core MVC application the control will be redirected to /home/fault.

Note you could use a lambda countenance in the UseExceptionHandler extension order in lieu of using a manner qualification feelr page. The pursueing code snippet illustrates how this can be achieved.

app.UseExceptionHandler(faultApp = gt;{
      faultApp.Run(async tenor = gt;{
            tenor.Response.StatusCode = (int)
HttpStatusCode.InternalServerError;
            tenor.Response.ContentType = "text/html";
            await tenor.Response.WriteAsync("lt;html
lang="en"gt;lt;bodygt; ");
            await tenor.Response.WriteAsync("An fault befallred...lt;brgt;lt;brgt; ");
            await tenor.Response.WriteAsync("lt;/bodygt;lt;/htmlgt; ");
            await tenor.Response.WriteAsync(new string( 512));
      });
});

The Developer Exception Page is useful by adding the Developer Exception Page middleware to the ASP.NET Core MVC request processing pipeline. The lapse template in ASP.NET Core MVC empowers this middleware for the Development environment only. For Production you can take gain of the UseExceptionHandler extension order to conaspect manner fault handling.