title: Pre-compiled Razor view to string with ASP.NET Core
body: <p>How is it possible to render a Razor view to a string, while using the new Razor pre-compilation from ASP.NET Core 1.1?</p>
  
  <p>The following code is from the example and the exception will be thrown because <code>viewResult.View</code> is null.</p>
  
  <pre><code>var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
  var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
  
  var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
  
  if (viewResult.View == null)
  {
      throw new ArgumentNullException($"{viewName} does not match any available view");
  }
  </code></pre>
  
  <hr>
  
  <p><a href="https://blogs.msdn.microsoft.com/webdev/2016/10/25/announcing-asp-net-core-1-1-preview-1/" rel="nofollow noreferrer">https://blogs.msdn.microsoft.com/webdev/2016/10/25/announcing-asp-net-core-1-1-preview-1/</a></p>
  
  <hr>
  
  <p>Old way (example): <a href="https://ppolyzos.com/2016/09/09/asp-net-core-render-view-to-string/" rel="nofollow noreferrer">File -> Razor -> Compile -> string</a></p>