Actionfilter Injection in ASP.NET MVC 5

ぃ、小莉子 提交于 2019-12-05 16:52:48
NightOwl888

Although your question is different, the answer is exactly the same as this one.

DI friendly attributes should never define any behavior. You need to separate the behavior out into a separate filter that can have its dependencies injected at application startup. This can be done by splitting your action filter attribute into 2 parts.

  1. An attribute that contains no behavior to flag your controllers and action methods with.
  2. A DI-friendly class that implements IActionFilter and/or IAuthenticationFilter that contains the desired behavior with a scanning implementation to check for the attribute.

Don't let Microsoft's marketing of ActionFilterAttribute fool you. That approach is completely hostile to DI.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!