【问题标题】:ASP.NET MVC RC returns wrong path for Url.ContentASP.NET MVC RC 返回 Url.Content 的错误路径
【发布时间】:2011-02-16 00:19:26
【问题描述】:

更新此问题在 ASP.NET MVC 的发布位中不存在

我有一个 ASP.NET MVC RC 应用程序,它在从 http://localhost:9002/ 运行的调试器下运行良好,但是当我将它发布到 http://localhost/Zot/ 时,对 Url.Content 的调用返回不正确的值。

我有类似的脚本标签

<script src="<%= Url.Content("~/Scripts/util.js") %>" ...

在已发布的网站中,这会产生:

<script src="Zot/Scripts/util.js" ...

代替

<script src="/Zot/Scripts/util.js" ...

<script src="Scripts/util.js" ...

我有样式表标签,例如:

<link href="~/Content/Site.css" runat="server" ...

产生正确的东西:

<link href="Content/Site.css" ...

关于为什么 Url.Content 失败的任何建议。我显然不能在&lt;script&gt; 标签上加上runat="server"

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    我倾向于使用 Rob Conery 的Script Registration helper

    public static string RegisterJS(this System.Web.Mvc.HtmlHelper helper, string scriptLib) {
      //get the directory where the scripts are
      string scriptRoot = VirtualPathUtility.ToAbsolute("~/Scripts");
      string scriptFormat="<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";
      return string.Format(scriptFormat,scriptRoot,scriptLib);
    }
    

    用法:

    <%= Html.RegisterJS("myscriptFile.js") %>
    

    正如您在示例中看到的,这使用 VirtualPathUtility 来解析 Scripts 目录的 url。这应该也有助于绕过标签汤的问题。

    【讨论】:

    • Rob,还请注意 Levi 的回复,因为显然这是在 RC2 中修复的错误。干杯。
    【解决方案2】:

    这应该已在 RC2 中修复。如果您正在使用 RC2 并且仍然遇到此问题,请在 http://forums.asp.net/1146.aspx 提交错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多