【问题标题】:Using ASP.NET MVC action parameters in custom attribute parameters在自定义属性参数中使用 ASP.NET MVC 操作参数
【发布时间】:2011-05-28 18:47:13
【问题描述】:

我想做这样的事情

[MyAttribute(Message="Please upgrade to view " + name)]
public ActionResult Details(string name)
{
    ....
}

我知道我可以从属性代码本身内部调用filterContext.ActionDescriptor.GetParameters(),但是有什么方法可以在控制器中使用它们吗?

【问题讨论】:

    标签: asp.net-mvc custom-attributes


    【解决方案1】:

    实现此目的的正确方法是使用自定义操作过滤器并在内部使用filterContext.ActionDescriptor.GetParameters() 或从RouteData 获取所需的参数。您不能在属性声明中包含动态值,因为属性表示在编译时烘焙到程序集中的元数据 => .NET 不允许您这样做。属性声明时只能使用静态或常量参数。

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 2011-07-29
      • 2012-10-26
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多