title: What is the best way to use Razor in a console application
body: <p>I know similar questions have been asked before, but the only answers are six years old, and the projects people refer to seem like they're not being maintained.</p>
  <p>I want to use Razor in a console app or a class library to render HTML. I would also like to have working Intellisense in the .cshtml files.</p>
  <p>Currently, I am able to jury-rig this by doing the following:</p>
  <ul>
  <li>Create console app.</li>
  <li>Add .cshtml file.</li>
  <li>In Properties, set the <code>Custom Tool</code> property to <code>RazorTemplatePreprocessor</code></li>
  </ul>
  <p>This causes Razor syntax to be recognized. So if you have <code>Test.cshtml</code>, it generates a class called <code>Test</code> that you can use like so:</p>
  <pre><code>var test = new Test();
  test.Model = &quot;Hi there&quot;;
  var html = test.GenerateString();
  
  Console.WriteLine(html);
  </code></pre>
  <p>This is just about good enough for my purposes. However, the actual coding experience within the .cshtml file is pretty broken:</p>
  <ul>
  <li>There are red squigglies all over the place complaining about:</li>
  <li>Not being able to use <code>var</code> because it's C# 2.</li>
  <li>Saying the type or namespace of various types can't be found.</li>
  <li>The above is especially true of types imported from other libraries.</li>
  <li>The <code>@model</code> keyword is not recognized.</li>
  <li>Intellisense sometimes works, sometimes doesn't.</li>
  </ul>
  <p>Weirdly, if you just ignore all of these errors, the HTML is actually generated correctly, including use of the libraries and @model keyword that the Razor engine complained about. However, this causes major mental clutter because if you have any number of .cshtml files, you very quickly get into hundreds and thousands of these errors mixed in with real errors.</p>
  <p>Is there anything I can do to cause the actual Razor coding experience to work more like it does in an ASP.NET web app?</p>