ASP.NET MVC 5 Filter – Define ActionFilterAttribute with Parameter

ASP.NET MVC 5 Filter – Define ActionFilterAttribute with Parameter

 
   [TestActionFilter(ID = 10, UserName = "")]
   public ActionResult Index() { … … }
 

Different Between System.Attribute & ActionFilterAttribute

Attribute would have only a set of property with value.
It is used to define some property for the Controller Action.

ActionFilterAttribute would have a set of property with value.
There is a set of EventHandler ( OnActionExecuting, OnActionExecuted, OnResultExecuting, OnResultExecuted ) which is defined to be overrided.
It is used to prepare some Filter Action from the Page Rendering LiveCycle.

LiveCycle of ActionFilterAttribute

OnActionExecuting – Execute Before the Action ( Function ) is started. ( Controller )
OnActionExecuted – Execute After the Action ( Function ) is completed. ( Controller )
OnResultExecuting – Execute Before the ActionResult – View Rendering is started ( View )
OnResultExecuted – Execute After the ActionResult – View Rendering is completed ( View )

 
   using System.Web.Mvc;
 
   public class TestActionFilter : ActionFilterAttribute
   {
      public int ID { get; set; }
      public string UserName { get; set; }
 
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
         base.OnActionExecuting(filterContext);
      }
 
      public override void OnActionExecuted(ActionExecutedContext filterContext)
      {
         base.OnActionExecuted(filterContext);
      }
 
      public override void OnResultExecuting(ResultExecutingContext filterContext)
      {
         base.OnResultExecuting(filterContext);
      }
 
      public override void OnResultExecuted(ResultExecutedContext filterContext)
      {
         base.OnResultExecuted(filterContext);
      }
   }
 
 
   [TestActionFilter(ID = 10, UserName = "")]
   public ActionResult Index() { … … }