ASP.NET MVC RC为Url.Content返回错误的路径

时间:2009-03-04 21:43:51

标签: asp.net asp.net-mvc

UPDATE 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失败原因的任何建议。我显然无法在runat="server"代码上添加<script>

2 个答案:

答案 0 :(得分:6)

我倾向于使用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。这也应该有助于绕过标签汤的问题。

答案 1 :(得分:2)

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