sign up! Channels Courses
 

Pedro Henrique Barbosa Fernandes - MrBool Space


20 last updates Pedro Henrique Barbosa Fernandes

Article - ASP.NET MVC Action Filters: HandleError, Authorize and OutputCache

Action Filter is an attribute that when added to an action of a controller, change the way their action is performed.


HandleError


Action Filter HandleError is used to redirect to a custom error page when an error is triggered by the action of the controller.


Listing 1: HandleError example



[CODE] [HandleError] public class HomeController : Controller { public ActionResult Index() { throw new NullReferenceException(); } public ActionResult About() { return View(); } } [/CODE]

When the NullReferenceException error happens in the action Index, the ASP.NET will find in a view called “Error”, in the controller views folder and renders it to the user. When the view called “Error” stay in the “Shared” Folder, will be shared with all controllers application.


We can also, redirect to the error pages, each type of error:


Listing 2: Redirect example



[CODE] [HandleError(ExceptionType = typeof(NullReferenceException), View = "NullError")] [HandleError(ExceptionType = typeof(SecurityException), View = "SecurityError")] public class HomeController : Controller { public ActionResult Index() { throw new NullReferenceException(); } public ActionResult About() { return View(); } } [/CODE]

Authorize


This Action Filte ...

Post view interrupted. To view full content, click here
4/10/2012 11:22:00 PM





 

pedro.hbf3@gmail.com

.Net Developer certified in Microsoft technologies. Net Framework 4.
Archive updates
 2012

Stats:
Total posts: 1
What people think:
Technical content:
People learning: