in my Asp.Net MVC 4 WebApi application I want to load additional WebApiControllers dynamically at a later time (after the WebApi initialization), which are in separate assemblies. Furthermore I want to add routes for those controllers at runtime.
I am wonder, if this is possible to do.
My goal is to build a web-app, where I can upload controllers (compiled assemblies) and the controllers will be automatically hosted within this application.
I've already tried to achieve that by implementing my own AssemblyResolver class, but (as far as I have seen), the AssemblyResolver is loaded once at initialization phase.
May be there is an option to "re-load" all controllers.
Any help will be appreciated!
Marius
ASP.NET Core supports creating web APIs using controllers or using minimal APIs.
It's recommended that API controllers in ASP.NET Core inherit from ControllerBase and add the [ApiController] attribute. Standard view-based MVC controllers should inherit from Controller . In both frameworks, controllers are used to organize sets of action methods.
Usually a Web API controller has maximum of five actions - Get(), Get(id), Post(), Put(), and Delete().
You could use Web API Dependency Resolver:
public class WebApiApplication : System.Web.HttpApplication
{
void ConfigureApi(HttpConfiguration config)
{
config.DependencyResolver = new MyDependencyResolver();
}
protected void Application_Start()
{
ConfigureApi(GlobalConfiguration.Configuration);
// ...
}
}
Using the Web API Dependency Resolver
Thanks for your answers.
I figured it out, it is not possible to do that, since all of the controllers are loaded once and are cached all over the time.
See HttpControllerTypeCache in DefaultHttpControllerSelector method InitializeControllerInfoCache(...).
In oder to do a type-cache refresh, I have to implement a custom HttpControllerSelector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With