ASP. NET core exception and error handling (8) _ Practical Tips

Source: Internet
Author: User
Tags stack trace throw exception
This article is mainly for you to introduce the ASP. NET core exception and error handling related data, with a certain reference value, interested in small partners can refer to

In this chapter, we will discuss exceptions and error handling. When an error occurs in an ASP. NET core application, you can handle it in a variety of different ways. Let's take a look at the addition of a middleware to handle exceptions, and this middleware will help us handle errors.

To simulate an error, let's go to the application, run, and if we just throw an exception, 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 what to configure your application,//Visit http://go.microsoft.com/fwlink/? linkid=398940 public void Configureservices (Iservicecollection services) {}//This method gets called by the run   Time.  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 the context.    Response.writeasync (msg);   });   }//Entry point for the application.  public static void Main (string[] args) = webapplication.run<startup> (args); } }

It will only throw a very generic exception message. Save the Startup.cs page and run your application.

You will see that we failed to load this resource. An HTTP 500 error occurred with an internal server error, and that page was not very helpful. It may be convenient to get some exception information.

Let's add another middleware usedeveloperexceptionpage.

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 the context. Response.writeasync (msg);  }); }

This middleware is a bit different from the others, and other middleware usually listens for incoming requests and responds to requests.

Usedeveloperexceptionpage will not be so concerned about what happens to incoming requests after the pipeline.

It just calls the next middleware, and then waits to see if there is an exception in the pipeline, and if there is an exception, this middleware will give you an error page about the exception.

Now let's run the application again. will produce an output as shown in the following screen.

Now if there is an exception in the program, you will see some exception information in the page that you want to see. You will also get a stack trace: Here you can see that the 37th line of Startup.cs has an unhandled exception thrown.

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

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.