ASP. NET Core exception and error handling (8), asp. netcore Processing

Source: Internet
Author: User

ASP. NET Core exception and error handling (8), asp. netcore Processing

In this chapter, we will discuss exception and error handling. When errors occur in ASP. NET Core applications, you can handle them in different ways. Let's take a look at how to add a middleware to handle exceptions. This middleware will help us handle errors.

To simulate an error, let's go to the application and run it. If we just throw an exception, let's see how the program works.

using Microsoft.AspNet.Builder; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; namespace FirstAppDemo {  public class Startup {   public Startup() {    var builder = new ConfigurationBuilder()    .AddJsonFile("AppSettings.json");    Configuration = builder.Build();   }   public IConfiguration Configuration { get; set; }      // This method gets called by the runtime.   // Use this method to add services to the container.   // For more information on how to configure your application,   // visit http://go.microsoft.com/fwlink/?LinkID=398940   public void ConfigureServices(IServiceCollection services) {   }     // This method gets called by the runtime.   // Use this method to configure the HTTP request pipeline.  public void Configure(IApplicationBuilder app) {    app.UseIISPlatformHandler();    app.UseRuntimeInfoPage();       app.Run(async (context) => {    throw new System.Exception("Throw Exception");    var msg = Configuration["message"];    await context.Response.WriteAsync(msg);    });   }      // Entry point for the application.   public static void Main(string[] args) => WebApplication.Run<Startup>(args);  } }

It only throws a very common exception information. Save the Startup. cs page and run your application.

You will see that we failed to load this resource. An HTTP 500 error occurs and an internal server error occurs. The page is not very helpful. It may easily obtain some exception information.

Let's add another middleware usemediaexceptionpage.

// This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) {  app.UseIISPlatformHandler();  app.UseDeveloperExceptionPage();  app.UseRuntimeInfoPage();   app.Run(async (context) => {   throw new System.Exception("Throw Exception");   var msg = Configuration["message"];   await context.Response.WriteAsync(msg);  }); }

This middleware is a little different from other ones. Other middleware usually listens to incoming requests and responds to the requests.

UseDeveloperExceptionPage does not care so much about what will happen in the pipeline after the incoming request.

It only calls the next middleware, and then waits to see if exceptions occur in the pipeline. If any, this middleware will give you an error page about this exception.

Now let's run the application again. The output shown on the following screen is generated.

If an exception occurs in the program, you can view the exception information on the page. You will also get a stack trace: here we can see that there is an unhandled exception throw in Startup. cs row 37th.

All these exception information will be very useful to developers. In fact, we may only want the exception information to be displayed when the developer runs the application.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.