13

我有一个ActionFilterAttribute,我想接受参数通过,但我不知道通过它们。动作过滤器动作参数

所以我的动作过滤器看起来像这样;

public class PreventAction : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     filterContext.Result = new RedirectResult("Home/Index"); 
    } 
} 

而我装饰我的动作是这样的;

[PreventAction] 
public ActionResult Ideas() 
{ 
    return View(); 
} 

现在我想添加一个参数,以便我可以像这样调用过滤器;

[PreventAction(myParam1 = "1", myParam2 = "2")] 
public ActionResult Ideas() 
{ 
    return View(); 
} 

任何人都知道如何做到这一点?

回答

25

只需将MyParam1MyParam2作为您的PreventAction类的属性即可。如果您需要参数(而不是可选),请将它们作为参数添加到PreventAction的构造函数中。

这是来自MSDN的简单属性类的quick tutorial

+1

+1太棒了!谢谢你,先生。 – griegs 2010-01-04 23:57:05

+1

很高兴帮助:) – womp 2010-01-05 00:05:52