问题
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