Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace the default IControllerFactory with my own implementation in asp.net core?

I want to create my own implementation of IControllerFactory and IActionInvoker in an ASP.NET core application. How do I replace the default implementation with my custom class?

I found the solution for older versions of MVC ControllerBuilder.Current.SetControllerFactory but cannot find a way to do that in ASP.NET core.

I believe I should configure this somewhere in Startup => ConfigureServices

like image 625
To Ka Avatar asked Mar 10 '23 20:03

To Ka


1 Answers

I found a solution by calling

services.AddSingleton<Microsoft.AspNetCore.Mvc.Controllers.IControllerFactory, MyOwnImplementationClassOfControllerFactory>();

right before calling services.AddMvc(); where MyOwnImplementationClassOfControllerFactory is the custom implementation of IControllerFactory that is derived form DefaultControllerFactory.

I used singleton, because I found that the default implementation uses the following call services.TryAddSingleton<IControllerFactory, DefaultControllerFactory>();

like image 126
To Ka Avatar answered Apr 27 '23 00:04

To Ka