Q:

aspnet_compiler cannot compile a web project that contains Razor views, if view engine namespace imports are defined in the global.asax

I've just attempted to build a new MVC project with Razor Views using our standard build script (rake, but that's not important). 
When I was developing the project I noticed that Razor doesn't currently respect namespace imports in the web.config, but does allow you to include namespaces in the global.asax. I'm actually happier with this as I don't see the need to be able to configure your imports if it then breaks your entire app.
However when using aspnet_compiler to compile the website it doesn't run the global.asax and so does not import the namespaces, and none of the views will compile. Is there any way around this, Ideally I'd like to try to compile my views as part of the build script to catch errors as soon as possible, but at this stage id be happy with just being able to publish the site from a build script.

A:

See this answer:
How to add extra namespaces to Razor pages instead of @using declaration?
In a nutshell, the Razor view engine has its own configuration section where you can import namespaces that you want to reference in Razor views.
<configSections>
  <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  </sectionGroup>
</configSections>

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="MyCustomHelpers" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

