Friendly Errors In MVC 5

In this article, I will be explaining 3 different ways to handle exceptions and display a friendly error page to the end user. Friendly Error Pages are the pages that you design to show the end user when any kind of not handled exception happens in your application and your expected result is not achieved. So, instead of presenting your end user with technical information regarding the exception, you show him a friend page. Also, presenting your end user with technical information is not a security best practice. 

So, let's see the three ways. 

Method 1: Custom Exception Filter

Custom Exception Filter

It is a filter that is called every time an exception occurs in the applied methods.

Why would I use a custom exception filter?

Because with a custom exception filter, you can handle in a generic way every kind of exception thrown by your actions and/or controllers. You may use custom exception filters to log your exceptions. 
 
Steps to achieve this:

Step 1

Create a new class named CustomExceptionFilter.
  1. public class CustomExceptionFilter : FilterAttribute, IExceptionFilter    
  2. {    
  3.     public void OnException( ExceptionContext filterContext )    
  4.     {    
  5.         filterContext.Result = new RedirectResult( "Home/About" );    
  6.         filterContext.ExceptionHandled = true;    
  7.     }    
  8. }    
What this custom exception does is to redirect to your About action in your Home Controller. Do not forget to set the ExceptionHandled equal to true; otherwise it will not take effect.

Step 2

Apply your custom exception filter to your controller/action.
  1. public class HomeController : Controller  
  2. {  
  3.     [CustomExceptionFilter]  
  4.     public ActionResult Index()  
  5.     {  
  6.         throw new Exception();  
  7.         return View();  
  8.     }  
  9.   
  10.     public ActionResult About()  
  11.     {  
  12.         ViewBag.Message = "Your application thrown an exception";  
  13.   
  14.         return View();  
  15.     }  
  16.   
  17.     public ActionResult Contact()  
  18.     {  
  19.         ViewBag.Message = "Your contact page.";  
  20.         throw new Exception();  
  21.         return View();  
  22.     }  
  23. }  

You can see that the exception filter is applied only to your Index Action so if you access your Contact action, you will be able to see the non-handled exception page.

Method 2: Web.Config configuration

Why would I handle an exception by web.config configuration? 

It is easy to configure and useful when you do not need to log or work on the exception.

Steps to achieve this:

Step 1

Include this in your web.config inside the system.web tag.
  1. <customErrors defaultRedirect="Error.cshtml" mode="On"></customErrors>  
Step 2

Update your Controller.
  1. public class HomeController : Controller  
  2. {  
  3.     [HandleError]  
  4.     public ActionResult Index()  
  5.     {  
  6.         throw new Exception();  
  7.         return View();  
  8.     }  
  9.   
  10.     public ActionResult About()  
  11.     {  
  12.         ViewBag.Message = "Your application description page.";  
  13.   
  14.         return View();  
  15.     }  
  16.   
  17.     public ActionResult Contact()  
  18.     {  
  19.         ViewBag.Message = "Your contact page.";  
  20.         throw new Exception();
  21.         return View();  
  22.     }  
  23. }  
Observation - Like the custom exception filter, you may choose if you want to handle the exceptions at the Controller or Action level. Also, here, the handled exception is applied only to your Index action, you may see the non-handled error in the Contact View.

Method 3: Global.Asax Application_error

What is the Application_error?

It is to override the global exception handler of the application. 
 
Why would I handle an exception by global.asax Application_error?

You do not need to set the places that you would like to handle the errors like before where you had to set the actions and controllers to be handled. Here, every exception will hit and be handled.

Steps to Achieve this:

Step 1

Update  your global.asax.
  1. protected void Application_Error( object sender, EventArgs e )  
  2.   {  
  3.       Exception exception = Server.GetLastError();  
  4.       Server.ClearError();  
  5.       Response.Redirect( "/Home/About" );  
  6.   }  
Like the custom exception handlers, it is very important to ClearError() in the Server.

What is done here is the redirection of non-handled exceptions to the About action in the Home Controller. 
 
Congratulation, you just learned three good ways of handling exceptions in your application and presenting friendly error pages to your end users. 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now