Чтение атрибута в OnAction Выполнение в asp.net mvc3 - PullRequest
5 голосов
/ 20 августа 2011

У меня есть действие и атрибут, как указано ниже, я переписал OnActionExecuting и хочу прочитать атрибут в этом методе

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


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

1 Ответ

10 голосов
/ 21 августа 2011

Попробуйте:

Контроллер

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);
}

Фильтр

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();
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...