ASP.NET Core Exception and Error Handling (8)_Practical Tips

微波
Release: 2017-06-28 14:06:57
Original
1526 people have browsed it

This article mainly introduces the relevant information of ASP.NET Core exceptions and Error handling in detail. It has certain reference value and interested friends can refer to it. In this chapter, we will discuss exception 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 look at handling exceptions by adding a

middleware

that will help us handle errors. To simulate an error, let's go to the application, run it, and see how the program behaves if we just

throw an exception

.

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

You will see that we failed to load this resource. There was an HTTP 500 error, Internal Server Error, and that page wasn't 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 context.Response.WriteAsync(msg); 
 }); 
}
Copy after login

This middleware is a little different from others. Other middleware usually listens for incoming requests and makes some responses to the requests.

UseDeveloperExceptionPage does not care so much about what happens to the incoming request later in the pipeline.

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

Now let’s run the application again. This will produce an output as shown in the screenshot below.

Now if an exception occurs in the program, you will see some exception information you want to see on the page. You'll also get a stack trace: Here you can see that an unhandled exception was thrown on line 37 of Startup.cs.

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

The above is the detailed content of ASP.NET Core Exception and Error Handling (8)_Practical Tips. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!