Skip to content

Class project why need add Microsoft.AspNetCore.MVC ProjectReference in aspnetcore2.2 if already included Microsoft.AspNetCore.App 2.2.0 #8546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shps951023 opened this issue Mar 15, 2019 · 6 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate

Comments

@shps951023
Copy link

shps951023 commented Mar 15, 2019

c# - aspnetcore web project import another project's controller - Stack Overflow

two project's csproj and version :

AspNetCore Web Project

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
  </ItemGroup>


  <ItemGroup>
    <ProjectReference Include="..\Controller\Controller.csproj" />
  </ItemGroup>
</Project>

Controller Project

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App"/>
  </ItemGroup>
</Project>

Controller Project Code

using Microsoft.AspNetCore.Mvc;

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return Content("Hello S.O");
    }
}

https://i.stack.imgur.com/zmYxW.png

Question

if run AspNetCore Web Project it'll get HTTP ERROR 404.

I need to add <PackageReference Include="Microsoft.AspNetCore.MVC"/> in cspoj
to run successfully.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App"/>
    <PackageReference Include="Microsoft.AspNetCore.MVC" />
  </ItemGroup>
</Project>

But Isn't NuGet | Microsoft.AspNetCore.App 2.2.0 already included Microsoft.AspNetCore.MVC,Why i need to add again?

enter image description here

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Mar 15, 2019
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us, @shps951023.
Controller discovery in referenced assemblies is based on the existence of the Microsoft.AspNetCore.Mvc package reference.
Microsoft.AspNetCore.App metapackage does indeed references that, so I would assume there's some SDK behavior in play here, resulting in the need of the explicit reference.
@pranavkm, this seems similar to #4332, but in this case we have a 2.2 framework. What else is going on here?
@shps951023 please provide a repro project so we can investigate this.

@shps951023
Copy link
Author

shps951023 commented Mar 20, 2019

Thanks for contacting us, @shps951023.
Controller discovery in referenced assemblies is based on the existence of the Microsoft.AspNetCore.Mvc package reference.
Microsoft.AspNetCore.App metapackage does indeed references that, so I would assume there's some SDK behavior in play here, resulting in the need of the explicit reference.
@pranavkm, this seems similar to #4332, but in this case we have a 2.2 framework. What else is going on here?

Newest tests have found problems with not only for netcoreapp2.2 , but also netcoreapp2.1 .

@shps951023 please provide a repro project so we can investigate this.

@pranavkm , @mkArtakMSFT thanks.

here is test project link :

shps951023/MutiFramewroksControllerLib

@pranavkm
Copy link
Contributor

The shared framework (Microsoft.AspNetCore.App) has some quirks which requires references to be defined in an very specific way. aspnet/Mvc#7882 (comment) has a bunch of exact rules required for working with transitive references to the shared framework. We had this documented in our migration guide, but that seems to be have been removed for some reason - dotnet/AspNetCore.Docs@5cdd18f#diff-c097133d54a637c1d605e857d9429c9f

@shps951023
Copy link
Author

shps951023 commented Mar 20, 2019

@pranavkm

Does it mean that if i make a Controller Lib Project then Microsoft recommend that you specify a version of each framework?

e.g

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.1;netcoreapp2.2;</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.2'">
	<PackageReference Include="Microsoft.AspNetCore.App" version="2.2.0"/>
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
  	<PackageReference Include="Microsoft.AspNetCore.App" version="2.1.1"/>
  </ItemGroup> 
</Project>

or just PackageReference Microsoft.AspNetCore.MVC

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.1;netcoreapp2.2;</TargetFrameworks>
  </PropertyGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.2'">
  	<PackageReference Include="Microsoft.AspNetCore.MVC" version="2.2.0"/>
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
    <PackageReference Include="Microsoft.AspNetCore.MVC" version="2.1.1"/>
  </ItemGroup> 
</Project>

@pranavkm
Copy link
Contributor

@shps951023 this is specifically to work with MVC's auto-discovery of controllers. We'd addressing some of this in 2.x. Also the recommendation is to not specify a version for Microsoft.AspNetCore.App in the top-level project, only for dependent projects that also target the shared framework. In addition, projects referencing Microsoft.AspNetCore.App should reference the WebSDK (Microsoft.NET.Sdk.Web)

@shps951023
Copy link
Author

@shps951023 this is specifically to work with MVC's auto-discovery of controllers. We'd addressing some of this in 2.x. Also the recommendation is to not specify a version for Microsoft.AspNetCore.App in the top-level project, only for dependent projects that also target the shared framework. In addition, projects referencing Microsoft.AspNetCore.App should reference the WebSDK (Microsoft.NET.Sdk.Web)

thanks

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate
Projects
None yet
Development

No branches or pull requests

4 participants