c# - 关于web api ActionFilterAttribute、FilterProvider、Dependency Injection的一些问题

标签 c# .net asp.net-mvc dependency-injection asp.net-web-api

我看到默认情况下,一个 MVC4 WebApi 项目有 2 个 IFilterProvider:ConfigurationFilterProviderActionDescriptorFilterProvider。在设置自定义 IFilterProvider 进行属性依赖注入(inject)时,首先从 Filters 集合中删除这两者是否合适?还是只删除 ActionDescriptorFilterProvider 并将 ConfigurationFilterProvider 留在集合中更合适?

其次,我看到在为特定操作构建 WebApi Http ActionFilterAttribute 之后,它似乎仍然存在。例如,当我将自定义操作过滤器放在 ApiController Get 方法上时,它仅在第一次调用该方法时构造。对 api 方法的后续调用似乎重用了相同的过滤器(构造函数断点仅在第一次调用期间命中)。这是正确的吗?为什么它不同于 MVC Action 过滤器,后者为每个方法调用创建一个新实例?

最佳答案

is it appropriate to remove both of these from the Filters collection first?

我已经成功地在 Action Filters 上实现了依赖注入(inject),而没有删除这些 FilterProvider 中的任何一个。事实上,我正在使用 StructureMap 扩展示例中的 ActionDescriptorFilterProvider

http://evolutionarydeveloper.blogspot.co.uk/2012/11/webapi-actionfilter-dependency.html

恐怕我无法阐明您的第二个问题:-)

关于c# - 关于web api ActionFilterAttribute、FilterProvider、Dependency Injection的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320846/

相关文章:

c# - 工具提示定位 C#

.net - 循环浏览 IE 8 中的所有选项卡

asp.net-mvc - 文件是在给定上下文中无效的方法

c# - 从 TimeZoneInfo.Local.StandardName 获取 UTC 偏移量

c# - 如何知道是否使用c#删除了一条记录

c# - 最坏情况操作系统时钟精度?

c# - Backbone 不会将模型值发送到服务器 api

c# - 如何隐藏数据表中的列

c# - MVC : Run a method on Application startup without calling it from Application_Start

c# - POST 到 C# 时 JSON 对象为空