问题标签 [asp.net-mvc-filters]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
3200 浏览

asp.net-mvc - filters.Add 与 FilterProviders.Providers.Add

我遇到了一个示例 MVC3 代码,它在Global.asax文件中有以下内容:

两者MyProvider1MyProvider2都是用 实现的IResultFilter,我很困惑为什么其中一个被添加到FilterProviders另一个被注册为全局过滤器。

为什么以及何时应该在 上添加自定义过滤器FilterProvider,为什么以及何时应该将它们注册为全局过滤器?

0 投票
1 回答
4621 浏览

asp.net-mvc - GlobalFilterCollection 的过滤器在 ControllerInstanceFilterProvider 的过滤器之前运行

我遇到了一个奇怪的行为,但我不确定我是否在正确的轨道上。

我有一个控制器,它覆盖基类OnException的方法。Controller

我还有一个自定义的 ExceptionFilter 如下:

然后,我将其注册为全局过滤器:

我期望控制器实例过滤器ControllerInstanceFilterProvider在全局过滤器之前运行,因为由is提供的过滤器的顺序Int32.MinValue和它们的范围是FilterScope.First.

此处还解释了:ASP.NET MVC 3 服务位置,第 4 部分:过滤器

但结果不同:

iisexpress.exe 信息:0:HandleErrorCustom 异常消息:06:56:49.972

iisexpress.exe 信息:0:ControllerFiltersController 异常:06:56:49.974

这是一个 ASP.NET MVC 4 应用程序,我不知道有任何更改会影响 ASP.NET MVC 3 的过滤器排序行为。我在这里缺少什么?

0 投票
1 回答
111 浏览

asp.net-mvc-3 - 从内部动作方法 catch 块执行 ErrorFilter

我正在使用全局 ErrorHandler 过滤器来记录未处理的异常,但我也希望能够使用相同的处理程序从 catch 块中记录已处理的异常。

我想访问全局过滤器并按照配置执行 ErrorHandler,但我在执行此操作时遇到了一些困难。我尝试了以下方法,但 Resharper 告诉我过滤器永远不可能是我的 ErrorLogFilter。

ErrorLogFilter 被添加到全局过滤器中,如下所示:

ErrorLogFilter(作为 Elfar 的一部分,类似于 Elmah,但用于 MVC)定义为:

关于如何做到这一点的任何建议?

0 投票
2 回答
5774 浏览

c# - IFilterProvider and separation of concerns

I have a situation where I need to inject some dependencies in a action filter, namely, my custom authorization provider in my custom authorization attribute. I stumbled upon a lot of people and posts who were saying that we should be separating the 'attribute metadata' from the 'behavior'. This makes sense and there is also the fact that filter attributes are not instantiated through the 'DependencyResolver' so it is difficult to inject the dependencies.

So I did a little refactoring of my code and I wanted to know if I had it right (I'm using Castle Windsor as the DI framework).

First off I stripped my attribute to contain only the raw data I need

I created a custom authorization filter that would contain the logic of determining if the current user has the proper authorization

The last part was to create a custom filter provider that would fetch this specific attribute and instantiate my custom filter passing its dependencies and any data it needs, extracted from the attribute.

The last step is the register the filter provider in the global.asax

So I'm wondering first, if I got the idea right and second, what could be improved.

0 投票
2 回答
27263 浏览

asp.net-mvc-4 - 在 ASP.Net MVC 4 和 Autofac 中注册全局过滤器

我有一个像这样的过滤器:

我需要为我的项目中的每个操作运行它

我试图在 GlobalFilters 中注册它,但我的属性没有被注入

我试过这个解决方案来注册我的过滤器,但它没有被调用

关于如何做到这一点的任何想法?

0 投票
1 回答
1772 浏览

c# - 没有服务定位器的 IFilterProvider 注入

我的目标:在 DI 提供 IFilterProvider 的情况下注入 IFilterProvider,但默认回退到全局 FilterProviders.Providers.GetFilters() 方法。

Internet 上有很多资源(包括“官方”Microsoft 资源)演示了如何将 IFilterProvider 接口注入到一个类中。但是,它们都使用服务定位器反模式(根据 Mark Seeman)来执行此操作。这是我找到的列表:

  1. http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvcdependencyinjection_topic4.aspx
  2. http://merbla.blogspot.com/2011/02/ifilterprovider-using-autofac-for-mvc-3.html
  3. http://thecodinghumanist.com/blog/archives/2011/1/27/structuremap-action-filters-and-dependency-injection-in-asp-net-mvc-3
  4. IFilterProvider 和关注点分离

那么这些方法有什么问题呢?它们都将 DI 容器注入到目标类中,而不是相反。如果您在类中看到诸如 container.Resolve() 之类的内容,那么您并没有将对象生命周期的控制权交给 DI 容器,而这正是控制反转的真正意义所在。

此外,尝试创建回退到全局 FilterProviders.Providers 实例的问题在于,尽管它与 IFilterProvider 接口具有相同的签名,但它实际上并未实现该接口。那么如何将全局静态成员作为逻辑默认值注入,并且仍然允许使用 DI 覆盖它呢?

0 投票
1 回答
220 浏览

asp.net-mvc - 如何获得已应用于特定控制器操作的授权过滤器列表?

在一个asp.net MVC 应用程序中,一位同事正试图在网站的布局控件中构建一组与角色相关的 UI 元素,而我的同事想要根据用户的角色关系制作一个显示或不显示的 html 扩展。

同事希望能够测试特定链接所链接的操作,并检查用户是否有权访问该链接。为此,最好针对控制器操作运行所有授权过滤器。

为了做到这一点,必须检索为特定操作注册的所有授权过滤器。

有谁知道如何仅获取与特定控制器操作关联的授权过滤器属性?

0 投票
2 回答
772 浏览

c# - IFilterProvider 的多个注册在 MVC5 中仍然有效吗?

我确信这段代码曾经可以工作。我在我的 Unity 配置中注册了一个 UnityFilterProvider 和一个 UnityGlobalFilterProvider。

我原以为它会同时获取两个 IFilterProvider,但如果它们都已注册,它只会获取 GlobalFilterProvider。如果唯一注册的是基本的 UnityFilterProvider,那么它可以正常工作。

但我不能让他们俩一起工作。

难道我做错了什么?这是否曾经有效,但在 MVC5 中无效?这从来没有用过,我应该做些不同的事情吗?

UnityFilterProvider.cs

UnityGlobalFilterProvider.cs

UnityConfig(截图)

有人有什么想法吗?

向提供者注册添加名称解决了问题

0 投票
1 回答
2153 浏览

c# - AuthorizeAttribute 单元测试依赖注入

我的目标是IAuthorizationFilter通过子类化AuthorizeAttribute和覆盖来创建 MVC 的自定义实现bool AuthorizeCore(HttpContextBase httpContext);

作为潮人,我想做这种 TDD 风格。
(仅仅因为我说我是一个时髦并不意味着你可以导航回来。是的,我看到了。)

另外,我想像这样使用 ninject注入构造函数的某些参数。

所以,我的问题是如何对这样的设置进行单元测试?

这是我的属性设置:

我试图测试void OnAuthorization(AuthorizationContext filterContext);暴露的方法,AuthorizeAttribute但没有什么可以断言的。filterContext没有以任何明显的方式改变。

我的下一步是创建一个带有[MyAuthorize("APermission")]on 动作的模拟类,然后像这样调用它:

但是由于我实际上并没有使用MyAuthorizeFilter它,所以没有调用实际进行授权检查的代码。

老实说,我不知道如何进行。

0 投票
1 回答
4499 浏览

c# - 在 MVC 中全局应用动作过滤器给出重定向循环异常

问题陈述:

如果会话过期或者用户尝试访问任何视图而不使用剃刀登录 MVC 4,我正在尝试将用户重定向到登录页面。

  • 如果我对每个操作方法都使用过滤器属性,它可以正常工作,而不是在 filter.config 中全局应用操作过滤器。
  • 我不想将此操作过滤器应用于每个操作方法。

我想全局应用它。如何通过全局应用动作过滤器来避免重定向循环?如何做到这一点?

登录控制器:

过滤器(动作过滤器):

过滤器 Config.cs: