加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET Core TestServer为Razor视图生成HTTP 50

发布时间:2020-12-16 07:20:46 所属栏目:asp.Net 来源:网络整理
导读:当我使用TestServer调用MVC端点来检查视图呈现时,它会导致HTTP 500内部服务器错误响应. 错误是: An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modi
当我使用TestServer调用MVC端点来检查视图呈现时,它会导致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="..MvcProjectMvcProject.csproj" />
  </ItemGroup>

</Project>

解决方法

正如 https://github.com/aspnet/Razor/issues/1212中所解释的,问题是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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读