Tilde Slash Paths Not Working in MVC 4

2020-03-13 04:44发布

As I understand it, a plain ol' "~/foo" path is supposed to work like @Url.Content("~/") in MVC 4. However, I'm trying to do this and getting many broken paths -- the tilde is still there when the HTML is output.

So, for example, I have this path in /Views/Shared/_Layout.cshtml:

<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

And the HTML delivered looks like this:

<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />

I'm pretty sure I have this running as an MVC 4 project, too. The following stuff's in the web.config:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  </assemblies>
</compilation>

...and at this point, I'm not sure what to look for next. Any suggestions?

8条回答
爷、活的狠高调
2楼-- · 2020-03-13 05:09

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.

查看更多
太酷不给撩
3楼-- · 2020-03-13 05:12

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.

查看更多
登录 后发表回答