Labels

Tuesday, April 28, 2009

MVC || Filters || Understanding Filters

Different Types of Filters

 

The ASP.NET MVC framework supports four different types of filters:

 

 

Authorization filters

Implements the IAuthorizationFilter attribute.

Used to implement authentication and authorization for controller actions

 

 

Action filters

Implements the IActionFilter attribute.

Contain logic that is executed before and after a controller action executes.

You can use an action filter, for instance, to modify the view data that a controller action returns.

 

 

Result filters

Implements the IResultFilter attribute.

Contains logic that is executed before and after a view result is executed.

For example, you might want to modify a view result right before the view is rendered to the browser.

 

 

Exception filters

Implements the IExceptionFilter attribute

Used to handle errors raised by either your controller actions or controller action results.

You also can use exception filters to log errors.

 

 

 

-          Each different type of filter is executed in a particular order. If you want to control the order in which filters of the same type are executed then you can set a filter's Order property.

-          The base class for all action filters is the System.Web.Mvc.FilterAttribute class.

-          If you want to implement a particular type of filter, then you need to create a class that inherits from the base Filter class and implements one or more of the IAuthorizationFilter, IActionFilter, IResultFilter, or ExceptionFilter interfaces.

 

Action Filers –

 

The ASP.NET MVC framework includes several Action filters:

 

  • OutputCache – This action filter caches the output of a controller action for a specified amount of time.
  • HandleError – This action filter handles errors raised when a controller action executes.
  • Authorize – This action filter enables you to restrict access to a particular user or role.

 

 

namespace MvcApplication1.Controllers
{
     public class DataController : Controller
     {
          [OutputCache(Duration=10)]
          public string Index()
          {
               return DateTime.Now.ToString("T");
          }
     }
}

 

 

 

Custom action filters can also be created. For example, you might want to create a custom action filter in order to implement a custom authentication system.

 

Custom Action Filter –

 

-          You need to use the base ActionFilterAttribute class.

-          This class implements both the IActionFilter and IResultFilter interfaces and inherits from the Filter class.

 

 

namespace MvcApplication1.ActionFilters
{
     public class LogActionFilter : ActionFilterAttribute
     {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
               Log("OnActionExecuting", filterContext.RouteData);       
          }
 
          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
               Log("OnActionExecuted", filterContext.RouteData);       
          }
 
          public override void OnResultExecuting(ResultExecutingContext filterContext)
          {
               Log("OnResultExecuting", filterContext.RouteData);       
          }
 
          public override void OnResultExecuted(ResultExecutedContext filterContext)
          {
               Log("OnResultExecuted", filterContext.RouteData);       
          }
 
          private void Log(string methodName, RouteData routeData)
          {
               var controllerName = routeData.Values["controller"];
               var actionName = routeData.Values["action"];
               var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
               Debug.WriteLine(message, "Action Filter Log");
          }
 
     }
}
     

 

 

 

Whenever any of the actions exposed by the Home controller are invoked – either the Index() method or the About() method – the stages of processing the action are logged to the Visual Studio Output window.

 

 

namespace MvcApplication1.Controllers
{
     [LogActionFilter]
     public class HomeController : Controller
     {
          public ActionResult Index()
          {
               return View();
          }
 
          public ActionResult About()
          {
               return View();
          }
     }
}
 

 

 

 

 

 

Hope this is clear now.

 

Thanks & Regards,

Arun Manglick || Senior Tech Lead

 

No comments:

Post a Comment