Home  > Resources  > Blog

Exception handling in ASP.NET MVC applications

 
February 27, 2016 by Faheem Javed
Category: Microsoft

For exception handling we have been using try..catch..finally block since forever in various languages. In this blog we will see how to handle the unhandled exceptions in ASP.NET MVC. Unhandled exceptions are the exceptions that are not handled using try..catch..finally block. For handling unhandled exceptions or for creating a global exception handler we have to perform following steps:

1. In web.config add following line in the system.web section

<customErrors mode=”RemoteOnly”></customErrors>

2. Before each controller add following annotation:

public ActionResult Index()

3. In Views folder create a sub-folder named Shared and create a file there named Error. Extension can be .cshtml (razor) or .aspx (web forms engine)

4. Optionally to the Error file add following code to display the actual error message:

@Model.Exception.Message

 

In case if you want to name the Error page something else then change the annotation before controller to . Replace custom_name with whatever your error page name is.

Follow Us

Blog Categories