43

I'm using ASP.NET MVC Core RC-2. I have a web project targeting the full .NET framework. I also have a separate class library in the solution, also targeting the full framework.

In the class library, I have a controller, marked with a route attribute. I have referenced the class library from the web project. This assembly references the nuget package Microsoft.AspNetCore.Mvc v. 1.0.0-rc2-final.

It was my understanding that this external controller would be discovered automatically, e.g. http://www.strathweb.com/2015/04/asp-net-mvc-6-discovers-controllers/

However this doesn't work for me- I browse to the URL of the route and I get a blank page and it doesn't hit my controller breakpoint.

Any ideas how to get this working?

Interestingly, it does seem to work for web projects targeting .NET Core Framework, referencing a class library also targeting .NET Core. But not for a web project targeting the full framework, referencing a standard .NET class library.

Note: this is MVC Core which is supposed to support this kind of scenario without any MVC<=4 routing overrides.

Community
  • 1
  • 1
booler
  • 705
  • 1
  • 7
  • 15
  • Possible duplicate of [How to register a Controller into ASP.NET MVC when the controller class is in a different assembly?](http://stackoverflow.com/questions/7560005/how-to-register-a-controller-into-asp-net-mvc-when-the-controller-class-is-in-a) – Oscar Jun 09 '16 at 12:27
  • 3
    This is MVC Core - brand new ASP.NET stack – booler Jun 09 '16 at 13:22

2 Answers2

75

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!

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
James Ellis-Jones
  • 3,042
  • 21
  • 13
7

I believe you are hitting the following known issue in RC2. https://github.com/aspnet/Mvc/issues/4674 (workaround is mentioned in the bug)

This has been fixed since then but will only be available in next release (unless you are ok with using nightly builds)

Kiran
  • 56,921
  • 15
  • 176
  • 161