Web
Analytics
Action Filters in MVC | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Action Filters in MVC

Action Filters are used to write processing loic before and after action execution.

Example: First Create Controller

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Diagnostics;

 

namespace WebApplication1.Controllers

{

    public class ActionFilterSamoleController : Controller, IActionFilter

    {

        //

        // GET: /ActionFilterSamole/

        public ActionResult Index()

        {

            return View();

        }

 

        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)

        {

            Trace.WriteLine("Action is Executed at " + DateTime.Now.ToString());

        }

 

        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)

        {

            Trace.WriteLine("Action is Executing at " + DateTime.Now.ToString());

            //throw new NotImplementedException();

        }

    }

}

Step 2: Attach View with Index

Step 3: Write web.config file



  



  

  

    

  


 

Step 4: Create log.txt file in d drive and execute application , you will get all trace information in log.txt.

Note : Creation of actionfilters in this way is limited scope , means these action filters are applicable only for this controller. In order to make these action filters across all controller we have to use Action filter attribute.

ActionFilter Attribute:

IN this process first we will create public class which will inherit ActionFilterAttribute, IActionFilter . After it we will write interface implementation logic and decorate our controller with newly created custom Action Filter.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Diagnostics;

 

namespace WebApplication1.Controllers

{

    [MyActionFilter]

    public class ActionFilterSamoleController : Controller, IActionFilter

    {

        //

        // GET: /ActionFilterSamole/

        public ActionResult Index()

        {

            return View();

        }

        public ActionResult Second()

        {

            return View("Index");

        }

 

    }

    public class MyActionFilter : ActionFilterAttribute, IActionFilter

    {

        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)

        {

 

          //To Get Descrition of invoking method

            Trace.WriteLine(filterContext.ActionDescriptor.ActionName + " was Executed at " + DateTime.Now.ToString());

        }

 

        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)

        {

//To Redirect somewhere else we can write

            filterContext.Result = new RedirectResult("https://yogeshdotnet.com");

            Trace.WriteLine(filterContext.ActionDescriptor.ActionName + " is Executing at " + DateTime.Now.ToString());

            //throw new NotImplementedException();

        }

    }

}

Download Source Code