Reader Level:
ARTICLE

ASP.NET MVC with Action Filters

Posted by Prashant Chaudhary Articles | ASP.NET MVC November 29, 2011
In this article you will see the use of a Action Filter in ASP.NET MVC
  • 0
  • 0
  • 10974

Introduction: While developing an ASP.NET MVC application, you may require the ability to apply various filters which work as restrictions to the controller as per the requirement of the user.

Steps to use Action Filter in an application  


Step 1:
Open Visual Studio 2010

111.png
 

  • Click on new from the menu strip
  • Choose project
     
12.png
 
  • Now click on ASP.NET MVC empty application from the installed template
Step 2: Add a controller in which we can apply action filter 
  • Right click on the controllers folder
  • Choose to Add controller
c222.png
 
  • And name the contoller
     
c111.png
 

Action Filter which cached the value for 5 seconds.

Code for the Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication13.Controllers
{
   {
     [OutputCache(Duration=5)]
       public string Index()
        {
          return DateTime.Now.ToString("T");
          }
      }
 }

nnnnewww23.png

ASP.NET MVC provide an interesting feature to deal with filters i.e it allows the user to use custom filters like:
1. Authorization Filters - This filter implements the IAuthorizationFilter as Attribute.
2. Action Filters - This filter implements the IActionFilter
as attribute.
3. Result Filters - This filter implements the IResultFilter
as attribute.
4. Exception Filter - This filter implements the 
IExceptionFilter as attribute.

Step 3: Now next step is to use a custom filter; we can create Log Action Filter
This Log Action Filter is an custom action filter that will provide a brief description about execution 

  • For this purpose create a new model class > right click on the model folder >  add class
M1.png
 

Code for this step will be :

using
System;
using System.Diagnostics;
using System.Web.Mvc;
using System.Web.Routing; 

namespace MvcApplication13.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");
           }
               }
                    }


Step 4: Now with a reference to the model class add a controller to it to get the view from action method
 

cc222.png
 

Now code the controller to use the LogActionFilter
 

using System.Web.Mvc;
using MvcApplication13.ActionFilters;
namespace MvcApplication13.Controllers
{
     [LogActionFilter]
     public class PacificController :
Controller
     {
      public ActionResult Index()
     {
      return
View();
      }
      public
ActionResult About()
      {
      return View();
      }
    }
  }

Output:

out22.png
 

Summary - We can use Action Filters as well as Custom Filters in ASP.NET MVC Application as per user's requirements.

COMMENT USING

Trending up