How to use Ninject to inject services into MVC 3 FilterAttributes?

前端 未结 1 1846
無奈伤痛
無奈伤痛 2021-01-19 18:31

I\'m writing a custom ErrorHandler attribute for my MVC project. I would like to inject an implementation of EventViewerLogger into that attribute.

I\'m using Ninje

相关标签:
1条回答
  • 2021-01-19 19:27

    Ninject.Web.Mvc has this functionality built in called "BindFilter" which lets you map an attribute (that takes some or no constructor args) to a filter (which has its constructor args injected). Additionally, you can use it to copy values from the attribute and inject them as constructor args to the filter if you want. It also lets you change scope on your filters to be per action or per controller etc so that they actually get re-instantiated (normal action filters don't get re-instantiated per request).

    Here's an example of how I've used it to do a UoW action filter.

    0 讨论(0)
提交回复
热议问题