- Exception Handling
- MVC Framework - Bundling
- MVC Framework - Ajax Support
- Advanced Example
- MVC Framework - Action Filters
- MVC Framework - Routing Engine
- MVC Framework - Layouts
- MVC Framework - Views
- MVC Framework - Controllers
- MVC Framework - Models
- MVC Framework - Folders
- MVC Framework - First Application
- MVC Framework - ASP.NET Forms
- MVC Framework - Architecture
- MVC Framework - Introduction
- MVC Framework - Home
MVC Framework Useful Resources
- MVC Framework - Discussion
- MVC Framework - Resources
- MVC Framework - Quick Guide
- Questions & Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MVC Framework - Exception Handpng
In ASP.NET, error handpng is done using the standard try catch approach or using apppcation events. ASP.NET MVC comes with built-in support for exception handpng using a feature known as exception filters. We are going to learn two approaches here: one with overriding the onException method and another by defining the HandleError filters.
Override OnException Method
This approach is used when we want to handle all the exceptions across the Action methods at the controller level.
To understand this approach, create an MVC apppcation (follow the steps covered in previous chapters). Now add a new Controller class and add the following code which overrides the onException method and exppcitly throws an error in our Action method −
Now let us create a common View named Error which will be shown to the user when any exception happens in the apppcation. Inside the Views folder, create a new folder called Shared and add a new View named Error.
Copy the following code inside the newly created Error.cshtml −
If you try to run the apppcation now, it will give the following result. The above code renders the Error View when any exception occurs in any of the action methods within this controller.
The advantage of this approach is that multiple actions within the same controller can share this error handpng logic. However, the disadvantage is that we cannot use the same error handpng logic across multiple controllers.
HandleError Attribute
The HandleError Attribute is one of the action filters that we studied in Filters and Action Filters chapter. The HandleErrorAttribute is the default implementation of IExceptionFilter. This filter handles all the exceptions raised by controller actions, filters, and views.
To use this feature, first of all turn on the customErrors section in web.config. Open the web.config and place the following code inside system.web and set its value as On.
<customErrors mode = "On"/>
We already have the Error View created inside the Shared folder under Views. This time change the code of this View file to the following, to strongly-type it with the HandleErrorInfo model (which is present under System.Web.MVC).
@model System.Web.Mvc.HandleErrorInfo @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name = "viewport" content = "width = device-width" /> <title>Error</title> </head> <body> <h2> Sorry, an error occurred while processing your request. </h2> <h2>Exception details</h2> <p> Controller: @Model.ControllerName <br> Action: @Model.ActionName Exception: @Model.Exception </p> </body> </html>
Now place the following code in your controller file which specifies [HandleError] attribute at the Controller file.
using System; using System.Data.Common; using System.Web.Mvc; namespace ExceptionHandpngMVC.Controllers { [HandleError] pubpc class ExceptionHandpngController : Controller { pubpc ActionResult TestMethod() { throw new Exception("Test Exception"); return View(); } } }
If you try to run the apppcation now, you will get an error similar to shown in the following screenshot.
As you can see, this time the error contains more information about the Controller and Action related details. In this manner, the HandleError can be used at any level and across controllers to handle such errors.
Advertisements