ASP.NET Core MVC controllers in separate assembly

Still an issue in ASP.Net Core 1.0, not sure if it’s by design now. Easiest solution is to do this in Startup.cs/ConfigureServices

services.AddMvc()
  .AddApplicationPart(typeof(<class in external assembly>).Assembly)
  .AddControllersAsServices();

AddApplicationPart explicitly includes the assembly in searches for controllers.
The call to AddControllersAsServices() will add all the discovered controllers into the services collection, and if you put a breakpoint after this line and inspect ‘services’, you will see in the collection all the controller types which have been found.

You might also want to check here: https://docs.asp.net/en/latest/migration/rc1-to-rtm.html#asp-net-5-mvc-compile-views as the discovery rules are now changed for controllers from RC1.

Also remember to use IActionResult instead of ActionResult!

Leave a Comment