Why Asp.net ASP.NET Controllers Are Not Produced from IController

ASP.net MVC4 has this β€œnew” web API concept for displaying CRUD functions in your data model. The base class for these controllers is the DataController , which comes from ApiController .

Unfortunately, this ApiController does not derive from IController , which is problematic since these requests cannot be processed through your regular user factory controller, since they must return an instance of IController .

Does anyone know the reason for this, since I don’t understand why you will have a controller in your MVC project that cannot be obtained from IController , since this violates your custom factory controller, since it cannot create an instance of each individual controller in your project .

In short, because of this inheritance, you cannot use your DI container for dependency injection.

+7
source share
2 answers

I sent the same question to Microsoft and received the following response from Eilon Lipton (thanks for this):

A brief history is that while ASP.NET MVC and ASP.NET Web API have many of the same design concepts (dependency injection, many interfaces for connecting user implementations, and ease of verification), they are based on various basic HTTP stacks. MVC is based on the System.Web stack, which has been used in ASP.NET for over 10 years. The Web API is based on the new System.Net.Http stack, which provides more flexibility for hosting (IIS + + unit test user hosts), as well as better testability and extensibility. If you compare IController and IHttpController, you will see that one uses System.Web up and down the stack, and the other does not use it at all.

Regardless, all the technologies created on the ASP.NET stack β€” MVC, Web Forms, Web APIs, Web pages (and Razor), will continue to work side by side in the application, allowing you to choose the right parts to create each part of your application . Although the individual component implementations within each part are not interchangeable, each of them can be connected to the same services, such as dependency injection systems, logging tools, data providers, etc.

As soon as we publish our post on this topic, I think this should clarify the situation.

+7
source

To run DI using ASP.Net WebAPI, you need to create a dependent converter for the DI container.

for ninject

The following work is carried out:
 public class NinjectDependencyResolver : System.Web.Http.Services.IDependencyResolver { private static IKernel m_Kernel; public NinjectDependencyResolver() { m_Kernel = new StandardKernel(); } public NinjectDependencyResolver(IKernel myKernel) { m_Kernel = myKernel; } public object GetService(Type serviceType) { return m_Kernel.TryGet(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return m_Kernel.GetAll(serviceType); } } 

then bind it in the Global.ascx file using:

 GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(yourKernel)); 

it looks like (but not exactly identical) to injecting MVC3 dependencies

+1
source

All Articles