ASP.NET Core Developers exception page

UseDeveloperExceptionPage Middleware

We talk about Startup class Configure () method in the following code :

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseFileServer();

    app.Run(async (context) =>
    {
     throw new Exception("您的请求在管道中发生了一些异常,请检查。");
        await context.Response.WriteAsync("Hello World!");
    });
}

If we use the above code to run our application, we do not see an exception, but see "Hello from Default.html page." If you know asp.net Core request processing pipeline works, you probably already know that we do not see the cause of the exception thrown us.

UseFileServerMiddleware combines UseDefaultFilesand UseStaticFilesmiddleware functions. In our previous video series, we have included a folder named wwwroot default.htmldefault html document.

Thus, the root URL request for the application, i.e., http://localhost:49119by a UseFileServerinversion processing middleware and pipe therefrom. So, in our Run()next middleware method registration request pipeline can not execute, so we will not see this middleware thrown.

Now, if we are to http://localhost:49119/abc.htmlrequest is made, we will see an exception. Because, in this case, UseFileServerthe middleware can not find the name abc.htmlof the file. It will continue to call the next middleware in the pipeline, we use in our example Run()method to register middleware. This middleware throws an exception, we see the exception details as expected.

If you have any experience with traditional asp.net, then you must be very familiar with this page. This is similar to the traditional asp.net in the yellow screen of death .

This Developer Exceptionpage contains Exception Details:

  • Stack traces, including the file name and line number that caused the exception
  • Query String, Cookies 和 HTTP headers

At present, the exception page of the "Query" tab, we see the "QueryString no data." If there is any request URL query string parameters, as shown below, you will see them in the "Query" tab.

http://localhost:48118/abc.html?country=person&state=islocked

Custom UseDeveloperExceptionPage middleware

Like most of the other middleware components in ASP.NET Core, we can also customize UseDeveloperExceptionPagethe middleware. Whenever you want to customize middleware components, always remember that you may have the appropriate OPTIONS对象. So, to customize the UseDeveloperExceptionPagemiddleware,

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

SourceCodeLineCountProperty specifies the number of lines of code results in code line before and after the exception to be included.

How UseDeveloperExceptionPage middleware work

UseDeveloperExceptionPageIntermediate position as the position of placed in front of the other middleware, because if the pipeline behind the middleware component throws an exception, it can handle an exception with the Developer Exceptionpage. Please refer to the following code: using the Run () after the registration middleware UseDeveloperExceptionPage()middleware approach. Therefore, in this case, the developer will not appear abnormal page. That's how it must be placed as soon as possible reasons for the request processing pipeline.

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);
    }
}

 

Welcome to add a personal Micro Signal: Like if thoughts.

I welcome the attention of the public numbers, not only recommend the latest blog for you, there are more surprises waiting for you and resources! Learn together and common progress!

Guess you like

Origin www.cnblogs.com/cool2feel/p/11453897.html