Register controller in another assembly in ASP.NET MVC Core 1.1

冷暖自知 提交于 2019-12-11 05:54:48

问题


I'm trying to make a shared library for a .NET MVC Core 1.1 app that contains a controller that needs to be registered in the host MVC app.

I've found some examples of this for earlier versions of .NET Core but none of them seem to work in the current release.

This was the approach that looked most promising:

services
    .AddMvc()
    .AddApplicationPart(typeof(OtherAssembly.Controller).GetTypeInfo().Assembly)
    .AddControllersAsServices();

I have not altered the standard routes so they still look like this:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

I have a controller called SimpleController in my other assembly and it has an index action.

I'm trying the url

/simple

but I'm getting a 404. Has something changed, or am I missing something. And is there a way in .NET Core to debug routes.

Update: I was trying to do this in a .NET Standard Class Library. As soon as I switched to using a .NET Core Class library all worked fine and as expected, without any extra setup and I was able to remove the AddControllersAsServices part.

I guess there was no strong reason, other than experimentation why I was using .NET Standard class library, but I'd still like to know why it doesn't seem like you can.

来源:https://stackoverflow.com/questions/44098200/register-controller-in-another-assembly-in-asp-net-mvc-core-1-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!