【问题标题】:ASP.NET MVC check for custom Attribute in Controller or ActionASP.NET MVC 检查控制器或动作中的自定义属性
【发布时间】:2013-01-17 02:22:12
【问题描述】:

考虑以下代码:

public class MyAttribute : Attribute {  }

[MyAttribute]
public class MyControlller : Controller
{
      //...
}

现在我有一个Global Action Filter,它可以让我得到一个ActionExecutingContext 对象。

我的问题是,在这里,我如何检查请求的 Controller 是否已用我的自定义 Attribute 装饰。

【问题讨论】:

  • 如果您的过滤器注册为“全局”过滤器,那么默认情况下,对所有控制器操作的所有请求都将通过您的过滤器。
  • @Matt,我明白这一点,这就是我想做的。但是我想排除一些基于Controller或Action的逻辑。因此,我认为最好为此类 Action 或 Controller 设置一个属性,并在 filter 方法中检查相同的属性以排除所需的逻辑。

标签: asp.net-mvc custom-attributes


【解决方案1】:

试试

actionExecutingContextInstance.Controller.GetType().GetCustomAttributes(typeof(MyAttribute), false).Length > 0)  

或者

actionExecutingContextInstance.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false).Length > 0)  

【讨论】:

    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多