Reputation: 22770
I have an ActionFilterAttribute
which I want to accept parameters through but I can't figure out pass them across.
So my action filter looks like this;
public class PreventAction : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Result = new RedirectResult("Home/Index");
}
}
And I decorate my action like so;
[PreventAction]
public ActionResult Ideas()
{
return View();
}
Now I want to add a parameter so I can call the filter like this;
[PreventAction(myParam1 = "1", myParam2 = "2")]
public ActionResult Ideas()
{
return View();
}
Anyone know how to do this?
Upvotes: 13
Views: 4839
Reputation: 116987
Just add MyParam1
and MyParam2
as properties of your PreventAction
class. If you require the parameters to be there (rather then being optional), add them as arguments to a constructor for PreventAction
instead.
Here's a quick tutorial of a simple attribute class from MSDN.
Upvotes: 25