Web项目导入另一个项目的控制器

Web项目导入另一个项目的控制器

本文介绍了aspnetcore Web项目导入另一个项目的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AspNetCore Web项目

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>

控制器项目

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

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

问题

如果运行AspNetCore Web Project,它将得到HTTP ERROR 404,我如何成功引用它?

Question

if run AspNetCore Web Project it'll get HTTP ERROR 404,how do i reference it successfully?

推荐答案

通过TestHost托管应用程序时,我遇到了同样的问题,解决方法是-在Startup.cs中手动添加包含控制器的程序集.

I had same issue when I hosted app via TestHost, workaround was - manually adding assembly containing controllers in Startup.cs.

service.AddMvc()
    .AddApplicationPart(typeof(HomeController).Assembly);

这篇关于aspnetcore Web项目导入另一个项目的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:43