asp.net-mvc - ASP.NET Core TestServer 导致 Razor View 出现 HTTP 500

标签 asp.net-mvc razor asp.net-core asp.net-core-mvc

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

错误是:

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

/CustomSubfolder/Views/_ViewImports.cshtml

One or more compilation references are missing. Possible causes include a missing 'preserveCompilationContext' property under 'buildOptions' in the application's project.json.

The type or namespace name 'MyNamespace' does not exist in the namespace 'Company.App' (are you missing an assembly reference?)

测试代码:

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);
        }
    }
}

我正在使用面向 .NET Framework 4.6.1 的 ASP.NET Core 1.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>

最佳答案

https://github.com/aspnet/Razor/issues/1212 中所述,问题是 Razor View 编译所需的 .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-mvc - ASP.NET Core TestServer 导致 Razor View 出现 HTTP 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464373/

相关文章:

c# - 使用 Excel = Microsoft.Office.Interop.Excel 编译错误

jquery - 使用 Bootstrap 和 MVC 创建子菜单类型下拉列表

asp.net-mvc-3 - "Bogus"Web 项目需要 razor 缺少 Web 组件消息

jquery - 如何从单击按钮呈现的部分 View 将 recordId 传递给模型构造函数

c# - 在 ASP.NET Core SignalR 中,如何将消息从服​​务器发送到客户端?

c# - 如何注销 ASP.NET Core Identity 中的其他用户

asp.net-core - 带有继承的 ASPNET Core 3.0 模型验证错误

c# - 从资源文件动态生成可用的本地化

asp.net-mvc - ASP.Net MVC3-强制使用大写字母的自定义数据注释

c# - 您可以在不需要集成测试的情况下自行测试 razor View 吗?