使用IOverrideFilter覆盖定制ActionFilters [英] Using IOverrideFilter to override custom ActionFilters

查看:124
本文介绍了使用IOverrideFilter覆盖定制ActionFilters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 IOverrideFilter 界面重写我的自定义全局过滤器,但它只是不工作! code看起来是如下:

I want to use IOverrideFilter interface to override my custom global filter but it is simply not working! Code looks like to be as follow:

public sealed class MyGlobalFilterExceptionAttribute : FilterAttribute, IOverrideFilter
{
    public Type FiltersToOverride
    {
        get { return typeof(ITest); }
    }
}

我的全局过滤器已实施 ITEST 接口。我知道我可以在我原来的全局过滤器实现的任务,但我想通过 IOverrideFilter 这样做。

My global filter has implemented ITest interface. I know I can implement the task in my original global filter but I would like to do so by IOverrideFilter.

任何想法??

推荐答案

MSDN的信息是不是它完全清楚,但<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.filters.ioverridefilter.filterstooverride(v=vs.118).aspx\"相对=nofollow> IOverrideFilter.FiltersToOverride 必须究竟执行下列操作之一:

The msdn info is not entirely clear about it but IOverrideFilter.FiltersToOverride must be exactly one of the following:


  • IActionFilter

  • 个IAuthorizationFilter

  • IAuthenticationFilter

  • 个IExceptionFilter

  • IActionFilter
  • IAuthorizationFilter
  • IAuthenticationFilter
  • IExceptionFilter

基本上,你不能覆盖特定的过滤器,只能覆盖上述类别之一的所有过滤器。看看一个在 ProcessOverrideFilters 方法中的的来源$ C ​​$ C

Basically, you cannot override specific filters, you can only override all the filters of one of the categories above. Have a look a the ProcessOverrideFilters method in the source code.

所以,让我们说你的 ITEST 过滤器类型 IActionFilter ,那么你的倍率是(同逻辑也适用于其他任何过滤器类):

So, let's say that your ITest filter is of type IActionFilter, then your override will be (The same logic would apply for any other filter category):

public Type FiltersToOverride
{
    get { return typeof(IActionFilter); }
}

您也可以使用predefined <一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.overrideactionfiltersattribute(v=vs.118).aspx\"相对=nofollow> OverrideActionFilters (以及其他类别的过滤器类似predefined覆盖属性)。

You could also use the predefined OverrideActionFilters (and similar predefined override attributes for other filter categories).

有关更精细的覆盖,可能需要制定具体的解决方案,<一个href=\"http://stackoverflow.com/questions/9953760/how-to-disable-a-global-filter-in-asp-net-mvc-selectively\">like这一个或写自己的过滤器提供商在这个非常漂亮的文章

For a more fine-grained override, you might need to develop specific solutions like this one or write your own filter provider as in this very nice article

这篇关于使用IOverrideFilter覆盖定制ActionFilters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆