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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With