Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Unable to define ASP.Net Core MVC controllers in .csproj class libraries

Open BillDines opened this issue 10 years ago • 11 comments

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

BillDines avatar Jul 07 '16 15:07 BillDines

Workaround for this as provided by @pranavkm in the issue referenced above:

In your startup:

services .AddMvc() .AddApplicationPart(typeof(ValuesControllerFromClassLib).Assembly)

BillDines avatar Jul 07 '16 15:07 BillDines

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 avatar Jul 07 '16 16:07 pranavkm

@pranavkm I don't think so, we might need to file a new one.

pakrym avatar Jul 07 '16 16:07 pakrym

Filed https://github.com/dotnet/cli/issues/3804 to track the CLI bug.

pranavkm avatar Jul 07 '16 16:07 pranavkm

@pranavkm @pakrym so this is an external issue?

Eilon avatar Jul 07 '16 17:07 Eilon

Appears so. We might want to verify that it works correctly once the CLI issue is fixed.

pranavkm avatar Jul 07 '16 17:07 pranavkm

https://github.com/dotnet/cli/issues/3804 isn't getting fixed so there's not anything more to do here.

pranavkm avatar Nov 17 '16 23:11 pranavkm

Maybe this is a feature? That all controllers from referenced libraries do not activate by default.

freerider7777 avatar Jul 18 '17 13:07 freerider7777

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:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

Thanks!

rynowak avatar Jul 18 '17 14:07 rynowak

This is an old issue from 2016. Defining a controller in another class library that you reference in your app should just work.

rynowak avatar Jul 18 '17 14:07 rynowak

Yes it works, tried that later. Thanks.

freerider7777 avatar Jul 19 '17 11:07 freerider7777