Asp.net mvc ASP.NET Core TestServer为Razor视图生成HTTP 500

Asp.net mvc ASP.NET Core TestServer为Razor视图生成HTTP 500,asp.net-mvc,razor,asp.net-core,asp.net-core-mvc,Asp.net Mvc,Razor,Asp.net Core,Asp.net Core Mvc,当我使用TestServer调用MVC端点来检查视图是否呈现时,它会导致HTTP 500内部服务器错误响应 错误是: 编译处理此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码 /CustomSubfolder/Views/_ViewImports.cshtml 缺少一个或多个编译引用。可能的原因包括应用程序的project.json中的“buildOptions”下缺少“PreserveComilationContext”属性 命名空间中不存在类型或命名空间名称“MyName

当我使用
TestServer
调用MVC端点来检查视图是否呈现时,它会导致HTTP 500内部服务器错误响应

错误是:

编译处理此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码

/CustomSubfolder/Views/_ViewImports.cshtml

缺少一个或多个编译引用。可能的原因包括应用程序的project.json中的“buildOptions”下缺少“PreserveComilationContext”属性

命名空间中不存在类型或命名空间名称“MyNamespace” “Company.App”(是否缺少程序集引用?)

测试代码:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace MvcProject.Tests
{
    [TestClass]
    public class ControllerTests
    {
        protected TestServer Server { get; }
        protected HttpClient Client { get; }

        public ControllerTests()
        {
            Server = new TestServer(new WebHostBuilder()
                .UseContentRoot("../../../../MvcProject")
                .UseStartup<Startup>());
            Client = Server.CreateClient();
        }

        [TestMethod]
        public async Task Action_Valid_Renders()
        {
            HttpResponseMessage response = await Client.GetAsync("http://localhost/");

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
    }
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用制度;
Net系统;
使用System.Net.Http;
使用System.Threading.Tasks;
命名空间MvcProject.Tests
{
[测试类]
公共类控制器测试
{
受保护的TestServer服务器{get;}
受保护的HttpClient客户端{get;}
公共控制员测试()
{
服务器=新的TestServer(新的WebHostBuilder()
.UseContentRoot(“../../../../MvcProject”)
.UseStartup());
Client=Server.CreateClient();
}
[测试方法]
公共异步任务操作\u有效\u呈现()
{
HttpResponseMessage response=await Client.GetAsync(“http://localhost/");
AreEqual(HttpStatusCode.OK,response.StatusCode);
}
}
}
我正在使用ASP.NET Core 1.1,目标是.NET Framework 4.6.1,我的MSTest.csproj文件如下所示:

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

  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.3" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
    <PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\MvcProject\MvcProject.csproj" />
  </ItemGroup>

</Project>

net461
假的
如中所述,问题在于Razor视图编译所需的
.deps.json
文件不会自动复制到测试项目的输出中

您可以将以下手动生成步骤添加到测试项目中以解决此问题

  <!--
    Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load() which looks next to a .dll
    for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site applications.
  -->
  <Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
    <ItemGroup>
      <DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
    </ItemGroup>
    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
  </Target>


好消息,ASP.NET Core 2.1路线图上似乎对此进行了修复。:-)在我的测试项目中,所需要的只是更改为