Introduction
Today, in this article we will discuss the exception handling concept in any ASP.NET Core application. Exception handling is one of the most important functionalities or part of any type of application which always needs to be taken care of and implemented properly. Exceptions are mainly means for the run time errors which occur during the execution time of the application. So, if this type of error is not properly handled, then the application will be terminated.
In ASP.NET Core, the concept of exception handling has been changed, and rather say, now it is in very much better shape to implement exception handling. For any API projects implementing exception handling against every action, the method is quite time-consuming and it also requires extra effort. So, for this purpose, we can implement the Global Exception handler so that all types of unhandled exceptions can be caught in this handler. The benefit of implementing a global exception handler is that we need to define this in one place. Through this handler, any exception that occurs in our application will be handled, even if we ann new methods or controllers. So, in this article, we will discuss how to implement global exception handling in the ASP.NET Core Web API.
Create ASP.NET Core Web API Projects in Visual Studio 2019
So, before going to discuss the global exception handler, first, we need to create an ASP.NET Web API project. For this purpose, follow the steps mentioned below,
- Now open the Microsoft Visual Studio and Click on Create a New Project
- In the Create New Project dialog box, select ASP.NET Core Web Application for C# and then click on the Next Button
- In the Configure your new project window, provide the project name and then click on the Create button.
- In the Create a New ASP.NET Core Web Application dialog, select API, and then click on Create Button.
- Ensure that the checkboxes “Enable Docker Support” and “Configure for HTTPS” are unchecked. We won’t be using these features.
- Ensure that “No Authentication” is selected as we won’t be using authentication either.
- Click OK
Use the UseExceptionHandler middleware in ASP.NET Core
So, to implement the global exception handler, we can use the benefits of the ASP.NET Core built-in Middleware. A middleware is indicated as a software component inserted into the request processing pipeline which handles the requests and responses. We can use the ASP.NET Core in-build middleware UseExceptionHandler to use as a global exception handler. The ASP.NET Core request processing pipeline includes a chain of middleware components. This pipeline in turn contains a series of request delegates that are invoked one after another. While the incoming requests flow through each of the middleware components in the pipeline, each of these components can either process the request or pass the request to the next component in the pipeline.
Through this middleware, we can get all the detailed information of the exception object like the Stack trace, inner exception, message, etc., and also return that information through the API to return as an output. We need to put the exception handler middleware inside the configure() of a startup.cs file. If we use any MVC-based application, then we can use the exception handler middleware just as below. This code snippet demonstrates how we can configure the UseExceptionHandler middleware to redirect the user to an error page when any type of exception has occurred.
Now, we need to check this exception message. For that purpose, open the WeatherForecastController.cs file and add the below action method to throw an exception
If we want to capture the details of the exception objects – i.e. the stack trace, message, etc then we use the below code as the exception middleware.
To check the output, just execute the API endpoint in any browser:
Define a custom Exception Middleware to handle Exceptions in ASP.NET Core API
Also, we can write our custom middleware to handle any type of exceptions. In this section, we demonstrate how to create a typical custom middleware class. Custom middleware also provides much more flexibility to handle exceptions. We can add a stack trace, an exception type name, an error code, or anything else that we want to include as a part of the error message. The below code snippet shows the typical custom middleware class.
In the above class, a request delegate is passed to any middleware. The middleware either processes this or passes it to the next middleware in the chain. If the request is unsuccessful, then an exception will be thrown, and then the HandleExceptionMessageAsync method will be executed within the catch block. So, let's update the Invoke method code as shown below.
Now, we need to implement the HandleExceptionMessageAsync method, as shown below.
Now, in the next step, we need to create a static class named ExceptionHandlerMiddlewareExtensions and add the below code within that class.
Now, in the last step, we need to turn on our custom middleware within the Configure method of the startup class, as shown below.
Conclusion
Exception handling is a mainly cross-cutting concept for any type of application. In this article, we discuss the implementation process of the global exception-handling concept. We can take the benefits of global exception handling in any ASP.NET Core application to ensure that every exception will be caught and return the proper details related to that exception. With global exception handling, we just need to write the exception-handling-related code for our entire application just in one place. Any suggestions feedback or queries related to this article are most welcome.
No comments:
Post a Comment