Exception Handling in ASP.NET Core – Session 57

preview_player
Показать описание
Error and Exception Handling - Session57
Home » Training Courses » ASP.Net Core MVC » Exception Handling in ASP.NET Core – Session 57

During this session, we discuss Exception Handling in ASP.NET Core in detail, also we will show you how to implement the changes on your web app to display your customized message when some exceptions happen. In session 56 we discussed exception handling a little bit, but in this session, we talk about it in depth.

404 Error Handling in ASP.NET Core
When we request a page on a website or a web app that does not exist within that area, HTTP code 404 will be returned to the browser. In some cases, browsers handle this type of error, but we can control the 404 error and display our desired message to the user.
In This session, we introduce three middleware that can handle 404 error to stop the browser to display its designed page. Also, as we explained earlier in session 7, we can use the middleware inside Configure in the Startup class.

The first method is UseStatusCodePages middleware, for implementing this method we just need to add this middleware in HTTP pipeline. It is easy to implement but it doesn’t meet our requirement and it just display another predefined message to the user.

The second method needs more implementation, for this method we need to use UseStatusCodePagesWithRedirects middleware, then we should create a respective Controller and Action Method to handle the error page. finally, we need to create a View in the Views/Shared folder to be accessible by all the controllers. This method works for us and we can see our customized page to handle 404 error. But when we use this method, the web app does not return 404 HTTP code to the browser which could be a little bit problematic. Also, when the requested page is redirected to the error page, the URL is changed to the Error page path which is not acceptable in some circumstances.

To solve the above-mentioned issues, we can implement the third method, all the implementation is the same as the above method, just we need to use UseStatusCodePagesWithReExecute middleware instead of the UseStatusCodePagesWithRedirects middleware.

Exception Handling in ASP.NET Core
Almost we can say always there is a possibility of unexpected exceptions in all the applications. If such an exception occurs in an application, it would stop working, and may the browser display a generic message regarding the exception. To control such a behavior, we can place our code in the Try / Catch block which can handle this situation.

Moreover, to display the proper message to the user, we can use UseExceptionHandler middleware and then add the respective action method in the defined controller.

Display Specific Message to the Specific Exception
In some circumstances, we are aware of some exceptions which can not be controlled due to the nature of the application. In this situation, we can show the user a more specific message and lead them to solve it. For instance, when we try to delete some Categories that have some children (cost records that include those categories), The EF Core throws an exception while it tries to save the changes in the database.

Thus, as we know the reason for the exception, we can handle it more accurately and display a more detailed message to the user.

Tutorial Playlist

Our Social Media

GitHub

Text Version of the course:
Рекомендации по теме