加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – OnAction中的读取属性在asp.net mvc3中执行

发布时间:2020-12-16 03:34:01 所属栏目:asp.Net 来源:网络整理
导读:我有一个动作和属性如下,我已经过度使用OnActionExecuting并想要读取该方法中的属性 [MyAttribute(integer)]public ActionResult MyAction(){}protected override void OnActionExecuting(ActionExecutingContext filterContext){ //here i want to read int
我有一个动作和属性如下,我已经过度使用OnActionExecuting并想要读取该方法中的属性

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


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

解决方法

试试吧:

调节器

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读