简体   繁体   English

如何在MVC4中禁用特定操作\\控制器的全局属性

[英]How to disable global attribute for a specific action\controller in MVC4

Ive got an attribute registered in global filters. 我已经在全局过滤器中注册了一个属性。 And I want it to be disabled for 2 actions. 我希望它能够在2个动作中被禁用。 How can I do this? 我怎样才能做到这一点?

There are a couple of ways to handle this: 有几种方法可以解决这个问题:

  1. If you have control over the code of the action filter you could modify it in order to test whether the controller action is decorated with some custom marker attribute (that you will create) and do not run in case it is. 如果您可以控制动作过滤器的代码,则可以对其进行修改,以便测试控制器动作是否使用某些自定义标记属性(您将创建)进行修饰,并且如果是,则不运行。 Then you will decorate the 2 controller actions with this marker attribute. 然后,您将使用此标记属性修饰2个控制器操作。
  2. If you do not have control over the code of this attribute you could write a custom filter provider by implementing the IFilterProvider interface and inside the GetFilters method that you're gonna need to implement test whether the controller action is decorated with some custom marker attribute and not include your global filter in the result. 如果您无法控制此属性的代码,您可以通过实现IFilterProvider接口编写自定义过滤器提供程序,并在GetFilters方法内部,您需要实现测试控制器操作是否使用某些自定义标记属性进行修饰不在结果中包含全局过滤器。 Then do not register the filter as a global action filter but register your custom filter provider instead. 然后,不要将过滤器注册为全局操作过滤器,而是注册自定义过滤器提供程序。 Filter providers are very often used to provide dependency injection into action filters. 过滤器提供程序通常用于向操作过滤器提供依赖项注入。 For example here's an article which illustrates that in details with StructureMap. 例如,这an article用StructureMap详细说明an article

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MVC4中,如何使用Url中的参数从控制器动作重定向到视图? - In MVC4, how to redirect to a view from a controller action with parameters in the Url? MVC4禁用一项操作的验证 - MVC4 Disable validation for one action 如何在特定控制器或动作中禁用骆驼保护套 - How to disable camel case in specific controller or action MVC4使用控制器隐藏或禁用字段 - MVC4 Hide or disable field using controller MVC4异步控制器操作未完成 - MVC4 Async Controller Action not completed 在MVC4控制器操作中,如何仅在传入模型时验证模型? - In an MVC4 controller action, how can I validate a model only when it's passed in? 我如何获取MVC4 Url.Action方法在控制器之前包含哈希 - How do I get MVC4 Url.Action method to include a hash before the controller 如何在同一控制器中获得MVC4 Web API以支持HTTP动词和“操作”方法? - How do I get MVC4 Web API to support HTTP Verbs and “Action” methods in the same controller? 如何通过名单 <int> 在查询字符串中到MVC4中控制器的操作 - How to pass List<int> in query string to a controller's action in MVC4 如何在asp.net MVC4中将默认值为False的复选框传递给控制器​​操作 - How to Pass default values as False of checkBox to controller action in asp.net mvc4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM