ASP.NET Core MVC controllers in separate assembly

后端 未结 2 668
礼貌的吻别
礼貌的吻别 2020-12-03 04:47

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 frame

相关标签:
2条回答
  • 2020-12-03 05:08

    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!

    0 讨论(0)
  • 2020-12-03 05:10

    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)

    0 讨论(0)
提交回复
热议问题