UseDeveloperExceptionPage middleware

Let’s talk about the following code in the Configure() method of the Startup class:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseFileServer(); App.run (async (context) => {throw new Exception(" your request has some exceptions in the pipeline, please check." ); await context.Response.WriteAsync("Hello World!" ); }); }Copy the code

If we run our application with the code above, we don’t see the exception, but instead see “Hello from the default.html page”. If you know how the ASP.NET Core request processing pipeline works, you probably already know why we didn’t see the exception we threw.

UseFileServer middleware combines the functions of UseDefaultFiles and UseStaticFiles middleware. Previously, we included a default HTML document called default.html in the wwwroot folder.

Therefore, the request to the application root URL, http://localhost:32702, is processed by the UseFileServer middleware and the request pipeline is reversed from there. Therefore, the next middleware in the request pipeline registered by our Run() method will not execute either, so we won’t see an exception thrown by this middleware.

Now, if we make a request to http://localhost:32702/a.html, we see an exception. Because, in this case, the UseFileServer middleware cannot find a file named A.html. It goes on to call the next middleware in the pipeline, which in our case is the one we registered with the Run() method. This middleware throws an exception, and we see the exception details as expected.

This Developer Exception page contains Exception details:

  • A stack trace, including the file name and line number that caused the exception
  • Query String, Cookies, and HTTP headers

Currently, on the “Query “TAB of the exception page, we see” no QueryString data “. If there are any Query string parameters in the request URL, as shown below, you will see them under the “Query “TAB.

http://localhost:32702/a.html?b=1&c=2
Copy the code

Custom UseDeveloperExceptionPage middleware

With ASP.NET in the Core, as in most other middleware components, we can also customize UseDeveloperExceptionPage middleware. Whenever you want to customize a middleware component, always keep in mind that you may have the appropriate OPTIONS object. So, to customize the UseDeveloperExceptionPage middleware,

DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions { SourceCodeLineCount = 10}; app.UseDeveloperExceptionPage(developerExceptionPageOptions);Copy the code

The SourceCodeLineCount property specifies the number of lines of code to include before and after the line that caused the exception.

How does UseDeveloperExceptionPage middleware work

UseDeveloperExceptionPage middleware position as much as possible, placed in front of the other middleware location, because if the pipes at the back of the middleware components throw an Exception, it can handle the Exception and display Developer Exception page. Please refer to the following code, using the Run () after registration of middleware UseDeveloperExceptionPage middleware () method. Therefore, in this case, the developer exception page will not be displayed. This is why it must be placed in the request processing pipeline as early as possible.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   //app.UseFileServer();

    app.Run(async (context) =>
    {
        throw new Exception("Some error processing the request");
        await context.Response.WriteAsync("Hello World!");
    });

    if (env.IsDevelopment())
    {
        DeveloperExceptionPageOptions developerExceptionPageOptions = new DeveloperExceptionPageOptions
        {
            SourceCodeLineCount = 10
        };
        app.UseDeveloperExceptionPage(developerExceptionPageOptions);
    }
}
Copy the code