Global exception handling in asp net core mvc



Global exception handling in asp net core mvc

Global exception handling in asp net core mvc

In this video we will discuss how to implement global exceptions handler in ASP.NET Core MVC

Text version of the video
https://csharp-video-tutorials.blogspot.com/2019/05/global-exception-handling-in-aspnet.html

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

Slides
https://csharp-video-tutorials.blogspot.com/2019/05/global-exception-handling-in-aspnet_22.html

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

ASP.NET Core Tutorial
https://www.youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQsfERmL_Jh4XmU

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

Throwing an Exception in ASP.NET Core

Consider the following Details action method in HomeController. We are deliberately throwing an exception using the throw keyword.

public ViewResult Details(int? id)
{
throw new Exception(“Error in Details View”);

// Rest of the code
}

UseDeveloperExceptionPage Middleware in ASP.NET Core

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

// Rest of the code
}

We have plugged in the DeveloperExceptionPage middleware into the HTTP request processing pipeline for the Development environment. So if we run the application in Development environment, then we see the developer exception page if there is an unhandled exception.

We discussed the significance and the use of DeveloperExceptionPage middleware in Part 13 of ASP.NET Core tutorial

As the name implies, the DeveloperExceptionPage middleware must be used only on the Development environment. Using this page on a non-development environment like Production for example is a security risk as it contains detailed exception information that could be used by an attacker. Also this exception page does not make any sense to the end user.

Unhandled Exception on Non-Development Environment in ASP.NET Core

On your local development machine to simulate running the application on production environment set ASPNETCORE_ENVIRONMENT variable to Production.

“ASPNETCORE_ENVIRONMENT”: “Production”

By default, if there is an unhandled exception in a non-development environment like production, we see the browser default page with http error 500. Error 500 means, there was an error on the server which the server did not know how to handle.

This default page is not very useful for the end user. We want to handle the exception and redirect the user to a custom error view which is more useful and meaningful.

Exception Handling in ASP.NET Core

Step 1 : For a non-development environment, add the Exception Handling Middleware to the request processing pipeline using UseExceptionHandler() method. We do this in the Configure() method of the Startup class. Exception Handling Middleware looks for ErrorController.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(“/Error”);
}

// Rest of the code
}

Step 2 : Implement the ErrorController that retrieves the exception details and returns the custom Error view. In a production application, we do not display the exception details on the error view. We instead log them to a database table, file, event viewer etc, so a developer can review them and provide a code fix if required. We will discuss logging in a later video.

Please note : IExceptionHandlerPathFeature is in Microsoft.AspNetCore.Diagnostics namespace.

Step 3 : Implement Error View

Comments are closed.