Using Ninject Attributes to Bind WebApi Filter Attributes

I am currently using ninject.mvc3 in my asp.net webapi project and it works great. Now I am trying to bind filter attributes, but BindFilter and BindHttpFilter missing in the IKernel interface when using the static method of the NinjectWebCommon class. If I add the Ninject.Web.WebApi package, it is there, but the project does not start (the problem is here , the solution says that Ninject.Web.WebApi is out of date), so I deleted it.

I also followed this github wiki and yet this.BindFilter not found. I have the latest nuget packages; Ninject (3.0.1.10), Ninject.Web.Common (3.0.0.7), Ninject.MVC3 (3.0.0.6)

Where is BindFilter or BindHttpFilter? Something has changed? How to bind filter attributes in asp.net web api? There are so many libraries and projects, and the documents do not tell you what is the current work and what is outdated ....

+6
source share
2 answers

The packages you use are correct as long as they do not include BindHttpFilter. The BindHttpFilter extension method refers to https://github.com/remogloor/Ninject.Web.WebApi , which currently does not work for the release of WebApi because it was written against the beta version of WebApi. To enter the WebApi filter, you need a BindHttpFilter; you cannot pass a WebApi filter to the BindFilter method.

This is https://github.com/ninject/Ninject.Web.WebApi/pull/1 , it seems the latest information that I can find in the update status.

You can also try another NuGet package http://nuget.org/packages/Ninject.Web.WebApi-rc , but I had some errors with it, now I don’t remember exactly which errors. In any case, after reading the discussion of the aforementioned request for traction, I refused this package.

In my case, I'm trying to implement a registration filter, so I'm just going to create a static method that I can call from the filter until support support is available.

+4
source

I have the same problem. 1) I install the last (but unstable) Ninject for Web.Api from the PM console using the command

 PM> Install-Package Ninject.Web.WebApi -Pre 

2) Then add this to the "using" section of NinjectWebCommon.cs

 using Ninject.Web.WebApi.FilterBindingSyntax; 

After these steps, my IKernel has a BindHttpFilter method. And everything works well

+4
source

Source: https://habr.com/ru/post/927081/


All Articles