Global exception handling in asp net core mvc

preview_player
Показать описание
In this video we will discuss how to implement global exceptions handler in ASP.NET Core MVC

Text version of the video

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.

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

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
Рекомендации по теме
Комментарии
Автор

@kudvenkat You are the best teacher in the world. No one can replace you. Please keep on sharing these type of series. Thanks for all you are doing for whole Software Development community and developers.

ankitmvp
Автор

Dear Kudvenkat
Thank you very much for valuable series
They are highly appreciated

talkathiriify
Автор

Thank you, the docs mention this but not the way you did, thank you again!

sipi
Автор

You are machine son great job as always THUMBS UP !!!

darkogele
Автор

اشكرك اخى الكريم على اى فيديو جديد لما فيه من إفادة للجميع ، وفى ميزان حسناتك ان شاء الله، ولى سؤال : هل الغرض من هذا الفيديو هو معالجة الاخطاء فى الدوت نت كور؟ كما فهمت، اما يوجد معنى اخر من هذا الفيديو؟

ramisaleiman
Автор

Hello, thank you for great tutorial. Im going throw all your lessons and it is prepared really well. But today Im slightly stucked, because my global exceptions dont catch any throw exception :/ Everything is set according tutorial. But any new throw exception, place anywhere in controllers or models are not caught by ExceptionHandler(env Production) or DeveloperExceptionPage(env Development) and app chrashes with comment undhandled exception. Thanks in advance.

danielbuchar
Автор

Thank u sir, am having doubt can u pls clear it, difference between exception filter in global error hanlder

dhamodharanm
Автор

Dear Kudvenkat
Thank you for amazing series. I learn a lot from you. But I need advice.
I have watched your c# and adonet videos and also other courses as well and also angular.. I wanna learn backend now asp net core. But without knpwing aspnet fundamental is it possible to directly dive into your aspnet core courses and learn? And also would it be easy to pass from your core courses to aspnet series if needed later? Thanks a lot

kerimbalayev
Автор

sir i visited to your channel but is 2-3 asp.net playlist I'm cofused which order should i followed can you plz guide me .

FOBZOGAMING
Автор

I had a 500 error on the Error view because I got fancy and added this at the top:
@{
ViewBag["Title"] = "Error"; }
I think it was because there was no model set on the page. Is there a way to make that work?

javaguitarist
Автор

why is my DeveloperExceptionPage middleware not working??? It says Exception unhandled.. pls help

carlosinigo
Автор

Hi Kudvenkat, Thanks for amazing tutorial :) I have a strange problem. when I change environment variable to Production, application do not use any layout we have created through the project. web browser shows buttons, links, pics any where it wants with different sizes. it seems it does not use bootstrap at all. would you help me in this. Thanks

shahabjalili
Автор

Hi. Instead of getting the developer exception page i am getting 'Exception Use-Unhandled System.Exception: 'Error in Details View' .
If I hit continue, i do get the exception page, but would like to understand why 'Exception Use-Unhandled System.Exception: 'Error in Details View' . occurs
Also the 'throw new exception... gives a warning 'unreachable code detected;
Can anyone help please?

lifetraveler
Автор

throw new Exception("Error in Details View");
Exception User Handled ???

Ruhgtfo
Автор

Middleware doesn't seem to be catching. Page is stuck on loading and the program just exits.

connerm
Автор

Hi...Is there any github repository for this project, thanks @kudvenkat

hanumansree
Автор

@kudvenkat You are the best teacher in the world. No one can replace you. Please keep on sharing these type of series. Thanks for all you are doing for whole Software Development community and developers.

mohamedmustafa
visit shbcf.ru