Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Filter ActionParameters

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?

like image 452
griegs Avatar asked Jan 04 '10 23:01

griegs


1 Answers

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.

like image 135
womp Avatar answered Oct 04 '22 15:10

womp