Follow by Email

Friday, December 5, 2014

Exception handling in mvc using HandleError Attribute

hi in this post i will show how to use handleError attribute for exception handling in mvc.

we can add handleError attribute around the whole controller class or just around the individual action methods.
next we need to add
<customErrors mode=”On”></customErrors> 
in the webconfig file.
10-25-2014 8-51-02 PM

Now in the view folder we will create a shared folder and in that we will add a view page called error.cshtml
so whenever there is any exception it will get redirected to this page.
we will create a strongly typed view which will have model HandleErrorInfo
error.cshtml code:
<!DOCTYPE html>
<meta name=”viewport” content=”width=device-width” />
Error Occured !!!
<table border=”1″>
Controller Name: @Model.ControllerName
Action Name: @Model.ActionName
Exception Details: @Model.Exception
model HandleErrorInfo code :
public class HandleErrorInfo
public string ActionName { get; set; }
public string ControllerName { get; set; }
public Exception Exception { get; set; }

Now run the application