Sunday 10 January 2021

Error Handling In MVC

In this article, I will show you different ways to handle exception in MVC, For this article, I’m using  VS 2019 community version.

 

Create MVC project name ErrorHandling.


Default and simple way to handle error in MVC and its default comes with when creating MVC application.



 

Use simple try catch and call Error view in catch block to show Error view to end user instead of actual error. Update index action shown below and throw error explicitly.

  


Run application to see error.


 

Now notice I commented, or we can say remove try catch block.

 


 


We already saw one way to handle this is to call error view from catch block for generic error. The same we can achieve by setting property in web.config file and it will work for all action method and no need to write try catch block for handling errors in all action methods, just one setting in root web.config file.


Run application and it loads error view, by just configuring customErrors property mode = ”On” in web.config file.



 

Till now we see exception handling ways but suppose we want to log exception messages then try catch method is repetitive and another way of enabling exception from web.config file is not providing way out to log error messages in file.

 

Now we want to improve error mechanism we want to show generic error to End User but somewhere in application we want to log actual error messages. We can achieve this with HandleErrorAttribute class which implement interface IExceptionFilter.  IExceptionFilter is one of filter in MVC which execute when error occurred for action method in MVC.


 

Let’s create class inside models folder.


Its customErrors attribute class, OnException method gets executed when error occurred in action method. Advantage of this CustomErrors class is you can log actual error inside OnException method. To implement this in controller class, just need to add this class as an attribute for action method or controller shown below.

 

In above screen CustomErrors attribute applied on action method level and if error occurred it will call and execute OnException method from CustomErrors class. Let’s apply CustomErrors attribute on class level.

If attribute applied on class level it means it applied all action methods inside controller class. In home controller CustomError attribute applied for all action method inside HomeController.cs  highlighted below.


There is small change in web.config file shown below.




Now try below URL from browser, notice there is no action method name test in Home controller.

https://localhost:44312/Home/Test


Page should show 404 page not found http error instead of above page. As action method is not available in Home controller. This is Http errors and we need to handle Http error separately.

 

In web.config file under custom error section, need to add http error with error tag and status code of http error with redirect( redirect to respective view if http status code matched).

Implement Http error views, open Error controller and add action method NotFound and Internal


Add view for NotFound and write down below code.


Now run application


Modify URL on browser


Press enter once URL modified and observe now it showing error related to Not found (404) http error.




0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home