AspNetCore Web项目
<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>
控制器项目
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>
</Project>
{{3}}
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller
{
public IActionResult Index()
{
return Content("Hello S.O");
}
}
如果运行AspNetCore Web Project,它将得到HTTP ERROR 404
,我如何成功引用它?
答案 0 :(得分:4)
当我通过TestHost托管应用程序时,我遇到了同样的问题,解决方法是-在Startup.cs中手动添加包含控制器的程序集。
service.AddMvc()
.AddApplicationPart(typeof(HomeController).Assembly);
答案 1 :(得分:1)
我想您在AspNetCore.csproj中的项目参考指向错误的路径。
请尝试更改
<ProjectReference Include="..\ControllerProject\Controller.csproj" />
到
<ProjectReference Include="..\Controller\Controller.csproj" />
编辑
使用nuget软件包Microsoft.AspNetCore.MVC
代替Microsoft.NETCore.App
应该可以解决此问题,而无需编辑startup.cs
文件。但是我个人更喜欢@khoroshevj