Unable to define ASP.Net Core MVC controllers in .csproj class libraries
I have an issue with a Web Api project created from the "Asp.Net Core Web Application (.Net Framework)" template which references a .Net 4.6.1 class library (.csproj) that uses the Microsoft.AspNetCore.MVC nuget package. Controllers defined in the class library are not picked up by the web project.
I have found some issues similar to this, but they have been fixed or closed and seem to be related to xproj project references only.
I'm not sure if this scenario should be supported right now, however it is something I would like to do to help me crack on with serious development in ASP.Net core. The tooling around xproj projects is not yet mature enough to easily fit into our development environment (source control, code analysis and unit testing all have problems). Whilst this is the case, the more code I can keep in .csproj the better!
To reproduce in VS2015 (update 3):
- Create a new project from the "Asp.Net Core Web Application (.Net Framework)" template
- Select "Web API" from the "Asp.Net Core Templates" Dialog
- Confirm this will run by hitting F5 - a browser will load and navigate to api/values
- In the same solution create a new .Net 4.6.1 class library (csproj)
- apply the Microsoft.AspNetCore.MVC nuget package to the class library (v1.0.0)
- Copy the ValuesController.cs from your web project into your class library.
- Delete ValuesController.cs from your web project.
- Add a reference to the new class library from your web project
- Hit F5 to Debug the web project again
- You get 404 Not found when navigating to api/values
Workaround for this as provided by @pranavkm in the issue referenced above:
In your startup:
services .AddMvc() .AddApplicationPart(typeof(ValuesControllerFromClassLib).Assembly)
It looks like the Dependencies node for Microsoft.Extensions.DependencyModel.Library entries for csproj based references is empty (and consequently we don't look for controllers in them). @pakrym would you know if there's a work item addressing this in the CLI?
@pranavkm I don't think so, we might need to file a new one.
Filed https://github.com/dotnet/cli/issues/3804 to track the CLI bug.
@pranavkm @pakrym so this is an external issue?
Appears so. We might want to verify that it works correctly once the CLI issue is fixed.
https://github.com/dotnet/cli/issues/3804 isn't getting fixed so there's not anything more to do here.
Maybe this is a feature? That all controllers from referenced libraries do not activate by default.
Hi, it looks like you are posting on a closed issue/PR/commit!
We're very likely to lose track of your bug/feedback/question unless you:
- Open a new issue
- Explain very clearly what you need help with
- If you think you have found a bug, include detailed repro steps so that we can investigate the problem
Thanks!
This is an old issue from 2016. Defining a controller in another class library that you reference in your app should just work.
Yes it works, tried that later. Thanks.