ASP.NET MVC with Action Filters

Introduction: While developing an ASP.NET MVC application, you may require the ability to apply various filters that 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.

Visual Studio 2010

Click on new from the menu strip

Choose project

Choose project

Now click on the ASP.NET MVC empty application from the installed template

Step 2. Add a controller in which we can apply an action filter.

Right-click on the controller's folder

Choose to Add a controller.

Controllers folder

And name the controller

Add name

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
{
    public class HomeController : Controller
    {
        [OutputCache(Duration = 5)]
        public string Index()
        {
            return DateTime.Now.ToString("T");
        }
    }
}

Protected mode

ASP.NET MVC provides an interesting feature to deal with filters i.e. it allows the user to use custom filters.

  • Authorization Filters: This filter implements the IAuthorizationFilter as an Attribute.
  • Action Filters: This filter implements the IActionFilter as an attribute.
  • Result Filters: This filter implements the IResultFilter as an attribute.
  • Exception Filter: This filter implements the IExceptionFilter as an attribute.

Step 3. Now next step is to use a custom filter; we can create a Log Action Filter.

This Log Action Filter is a custom action filter that will provide a brief description of an execution.

For this purpose create a new model class > right-click on the model folder and add the class.

Add class

The 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 the action method.

Action method

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

Output

Summary

We can use Action Filters as well as Custom Filters in ASP.NET MVC Applications as per the user's requirements.


Similar Articles