ASP.NET Core TestServer在Razor视图中产生HTTP 500 [英] ASP.NET Core TestServer results in HTTP 500 for Razor views

查看:96
本文介绍了ASP.NET Core TestServer在Razor视图中产生HTTP 500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用TestServer调用MVC终结点以检查视图是否呈现时,它会导致HTTP 500 Internal Server Error响应.

When I use the TestServer to call an MVC endpoint to check that the view renders, it results in a HTTP 500 Internal Server Error response.

错误是:

在编译处理该请求所需的资源期间发生错误.请查看以下特定的错误详细信息,并适当地修改您的源代码.

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

/CustomSubfolder/Views/_ViewImports.cshtml

缺少一个或多个编译参考.可能的原因包括应用程序的project.json中"buildOptions"下缺少"preserveCompilationContext"属性.

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

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

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文件如下所示:

I'm using ASP.NET Core 1.1 targeting .NET Framework 4.6.1, and my MSTest .csproj file looks like:

<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视图编译所需的.deps.json文件没有自动复制到测试项目的输出中.

As explained in https://github.com/aspnet/Razor/issues/1212, the problem is that .deps.json files required for Razor view compilation are not automatically copied over to the output of the test project.

您可以将以下手动构建步骤添加到测试项目中,以解决该问题.

You can add the below manual build step to the test project to resolve the issue.

  <!--
    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 TestServer在Razor视图中产生HTTP 500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆