Tilde Slash Paths Not Working in MVC 4

I had this problem when I cut and paste some example code into a view. Turned out I had the wrong type of tilde!

@{
Layout = "∼/Views/_BasicLayout.cshtml";
}

vs

@{
Layout = "~/Views/_BasicLayout.cshtml";
}

Subtly different - sod to find


My guess is that you are still running Razor 1 (MvcWebRazorHostFactory is < 4.0).

Verify the web.config in your Views folder looks like this...

 <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <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" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

This could be as a result of a bug in Razor V2, where an apostrophe / single quote in an HTML comment breaks resolution of ~ paths.

The workaround is to use Razor comments instead of HTML comments. I.e., replace <!-- Here's your comment--> with @* Here's your comment *@.

Sorry this is a long shot, as I've no idea if you have HTML comments, let alone ones containing single quotes.


Reinstalling MVC 4 (RC) using the standalone installer here solved this problem for me. I still don't understand what caused the problem, but I can live with that.