· Authorization
- Implements IAuthorizationFilter interface.
- Provides AuthorizeAttribute as the default class implementation.
- Executes before any other filter or the action method.
- Implements IActionFilter.
- Provides ActionFilterAttribute as the default class implementation.
- Executes before and after the action method.
- Implements IResultFilter interface.
- Executes before and after action result is executed.
- Implements IExceptionFilter interface.
- Provides HandleErrorAttribute as the default class implementation.
- Executes only if exception is thrown by action method or an action result.
In this article, we will see how to implement the Custom Exception Filter in MVC. An assumption for this article is that the Model layer is creating using ADO.NET EF. The article demonstrates a custom exception filter used for ‘UpdateException’.
Some to-do’s before using the code:
- Create MVC 3 application using VS2010.
- Add the model layer in the application using ADO.NET EF.
- Generate Controller and implement logic for performing Index, Create, Edit and Delete actions.
- Run and test the functionality.
- Create a Company Database in SQL Server with Department table.
using System.Data; using System.Web.Mvc; using MVC3_CustomException_Filter.Models; namespace MVC3_CustomException_Filter.CustomFilterRepository { /// <summary> /// Class used to Handle the Exception for /// Duplicate Entry For Primary Key /// This Handles System.Data.UpdateException /// </summary> public class ModelExceptionAttribute : FilterAttribute,IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (!filterContext.ExceptionHandled && filterContext.Exception is UpdateException) { var routeData = filterContext.RouteData; var controllerName = routeData.Values["controller"].ToString(); var actionName = routeData.Values["action"].ToString() ; filterContext.Result = new RedirectResult("~/ErrorPages/" + controllerName + "_" + actionName + ".html"); filterContext.ExceptionHandled = true; } } } }
The class ‘ModelExceptionAttribute’ is inherited from ‘FilterAttribute’ abstract class and implements the ‘IExceptionFilter’ interface. This interface provides method for exception filter. The method ‘OnException’ accepts ‘ExceptionContext’ object. This class provides exception context for Handling error. The code checks for the ‘UpdateException’. The result property of the ‘ExceptionContext’ class redirects to the Html page.
Step 2: Add a new Folder in the project and name it as ‘ErrorPages’. In this folder, add html pages with a name format like ‘ControllerName_ActionName.html’, e.g. for the Create action method of controller class, the html page will be ‘Department_Create.html’.
Note: You can go for another approach as per your application needs. Design the html page as per your requirement.
Step 3: In the department controller class, on the Create action, apply the custom filter as shown below:
[ModelException] public ActionResult Create(Department department){ try { if (ModelState.IsValid) { //your code here return RedirectToAction("Index"); } } catch (Exception) { throw; } return View(department); }
Step 4: Run the application and navigate to the create action of the department controller class. Try to re-add the primary key value for the department number (DeptNo). An update exception will occur:
Press F5 and an html page will appear as shown below:
Conclusion:
Using Custom filters in MVC 3, you can easily incorporate custom, value added business logic e.g. Custom Security, Custom Exception, Custom Logging in your application which makes your application more flexible and maintainable.
Download the entire source code
You made some decent points there. I looked on the internet for the issue and found most individuals will go along with with your website.
ReplyDeleteAn impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!
ReplyDelete