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