2009-03-04 78 views
7

UPDATE這個問題不會在ASP.NET MVC的釋放位存在錯誤的道路ASP.NET MVC RC返回Url.Content

我有這下運行良好的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失敗的任何建議。我顯然不能在<script>標籤上放置runat="server"

回答

6

我傾向於使用羅布科納的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來解決腳本目錄的URL。這也應該有助於繞過標籤湯的問題。

+0

謝謝!這很好。 – 2009-03-05 16:15:03