在OnAction中读取属性在asp.net mvc3中执行

Rus*_*ova 5 asp.net-mvc asp.net-mvc-3

我有一个动作和属性如下,我已经过度骑行 OnActionExecuting并希望在该方法中读取属性

[MyAttribute(integer)]
public ActionResult MyAction()
{
}


protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    //here i want to read integer passed to action using Attribute
}
Run Code Online (Sandbox Code Playgroud)

vla*_*mir 10

试试吧:

调节器

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
  foreach (var filter in filterContext.ActionDescriptor.GetCustomAttributes(typeof (MyAttribute), false).Cast<MyAttribute>())
  {
    var desiredValue = filter.Parameter;
  }

  base.OnActionExecuting(filterContext);
}
Run Code Online (Sandbox Code Playgroud)

过滤

public class MyAttribute : FilterAttribute, IActionFilter
{
  private readonly int _parameter;

  public MyAttribute(int parameter)
  {
    _parameter = parameter;
  }

  public int Parameter { get { return _parameter; } }

  public void OnActionExecuted(ActionExecutedContext filterContext)
  {
    //throw new NotImplementedException();
  }

  public void OnActionExecuting(ActionExecutingContext filterContext)
  {
    //throw new NotImplementedException();
  }
}
Run Code Online (Sandbox Code Playgroud)