ASP.NET Kernel - Override Standard ControllerFactory

I have a very specific situation where I would like to override the standard ASP.NET Core ControllerFactory. I would like to do this because I want to have full control over which controller I process each request with.

Scenario:

  • The request comes with a specific subdomain
  • Based on this subdomain, I want to enable a Generic type controller in a factory

For instance:

  • category.website.com is called
  • We see this type of category and will use the generic HomeController<T> , using DI to enter the category so that the type is HomeController<Category>
  • HomeController<Category> will use some common methods for methods of type category to render the main page.

If I’m lucky this link , when the application starts, a factory of type DefaultControllerFactory registered. This does not seem to be surmountable.

Any idea how I will go about this? The most logical for us is to use the old version of ASP.NET MVC, which allows you to install your own ControllerFactory, but we would lose features, such as the ability to use SpaServices to pre-use our Angular application.

+5
source share
1 answer

Register your own implementation with ConfigureServices after calling AddMvc:

 services.AddSingleton<IControllerFactory, MyCustomControllerFactory>(); 

Thus, it will be called whenever a controller is created.

+7
source

All Articles