Search code examples
asp.net-mvc-4razorprecompile

Precompile not working with Razor views in MVC 4


I'm trying to precompile razor views but during the build VS2010 throws errors such as "@ViewBag does not exist in the current context." The project builds and runs fine when views are not precompiled.

In order to turn on view compilation I set the following node in the project file:

<MvcBuildViews>true</MvcBuildViews>

The section below exists in the Views Web.config file.

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Xml"/>
        <add namespace="System.Xml.Linq"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Any ideas why the project won't build?


Solution

  • Geeze, do I ever feel like an idiot. Turns out there were some cshtml files in a _temp folder that resided inside of the project folder. Even though the folder was excluded from the solution the compiler was picking up the files contained therein. Moving that _temp file out of the project folder fixed the issue.