Thursday, March 28, 2013

Costume Attribute WIth paramerters

Hi All ,Here i try to explain how to create Costume attribute and pass parameter to the costume attribute in mvc


Create a class which inherited from ActionFilterAttribute (or any filter classes)


public class CheckNames : ActionFilterAttribute
    {
        public string name { get; set; }
        public int id { get; set; }

        public override void OnActionExecuting(ActionExecutingContext actionContext)

        {
            var actn = actionContext.ActionDescriptor.ActionName;
            var cntrl = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;

        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
    }


And in controller you can directly call


        [CheckNames(name = "asd", id = 1)]
        public ActionResult Index()
        {
            return View();
        }




No comments: