title: ASP.NET Core TestServer results in HTTP 500 for Razor views
body: <p>When I use the <code>TestServer</code> to call an MVC endpoint to check that the view renders, it results in a HTTP 500 Internal Server Error response.</p>
  
  <p>The error is:</p>
  
  <blockquote>
    <p>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.</p>
    
    <p>/CustomSubfolder/Views/_ViewImports.cshtml</p>
    
    <p>One or more compilation references are missing. Possible causes include a missing 'preserveCompilationContext' property under 'buildOptions' in the application's project.json.</p>
    
    <p>The type or namespace name 'MyNamespace' does not exist in the namespace
    'Company.App' (are you missing an assembly reference?)</p>
  </blockquote>
  
  <p>Test code:</p>
  
  <pre><code>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&lt;Startup&gt;());
              Client = Server.CreateClient();
          }
  
          [TestMethod]
          public async Task Action_Valid_Renders()
          {
              HttpResponseMessage response = await Client.GetAsync("http://localhost/");
  
              Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
          }
      }
  }
  </code></pre>
  
  <p>I'm using ASP.NET Core 1.1 targeting .NET Framework 4.6.1, and my MSTest .csproj file looks like:</p>
  
  <pre><code>&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
  
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;net461&lt;/TargetFramework&gt;
      &lt;IsPackable&gt;false&lt;/IsPackable&gt;
    &lt;/PropertyGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.3" /&gt;
      &lt;PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" /&gt;
      &lt;PackageReference Include="MSTest.TestAdapter" Version="1.1.18" /&gt;
      &lt;PackageReference Include="MSTest.TestFramework" Version="1.1.18" /&gt;
    &lt;/ItemGroup&gt;
  
    &lt;ItemGroup&gt;
      &lt;ProjectReference Include="..\MvcProject\MvcProject.csproj" /&gt;
    &lt;/ItemGroup&gt;
  
  &lt;/Project&gt;
  </code></pre>