How can I add Web Api Controller to a different assembly? I want to separate the web API controllers from my MVC application.
WebApi does that out the box.
The DefaultAssembliesResolver class is the one used to load the assemblies to look for ApiControllers. Here is the source code:
public class DefaultAssembliesResolver : IAssembliesResolver {
public virtual ICollection<Assembly> GetAssemblies() {
return AppDomain.CurrentDomain.GetAssemblies().ToList();
}
}
The trick is to load your assembly to the AppDomain before the first call to a web.api service.
Call any method of a class in the target assembly or simply load a type from it. Ex:
var x = typeof (YourApiControllerInAnotherAssembly);
If you're using an older version of WebApi, try implementing IAssembliesResolver and changing the default implementation of it like this:
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver),
new YourCustomAssemblyResolver());
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