2010-10-12 94 views
7

我已經看到了很多的使用Url.Content引用的JavaScript形式MasterPages例子,在MVC 2在ASP.net MVC使用Url.Content 2.0

<script src="<%: Url.Content("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script> 

但在運行時我有故障,

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

編譯器錯誤消息:CS0103:名稱'Url'在當前上下文中不存在。

我還沒有找到Url名稱空間在哪裏聲明,應該使用額外的程序集?

VS2010,IIS 7,ASP.net MVC 2.0

+0

如果取出該行,您的應用程序是否正常運行? – awrigley 2010-10-12 08:24:18

回答

8

確保您的母版頁繼承System.Web.Mvc.ViewMasterPage

+0

真的!它開始工作! – 2010-10-12 13:29:41

1

刪除編輯,如單引號獲得儘可能字符文字處理,因此會導致錯誤 '的文字太多字符'。恕我直言,最可能的原因仍然是一個錯字。

原崗位(仍然站立在重類UrlHelper):

Url.Content():這裏鏈接一個輔助方法,有點像HTML或AJAX輔助。

在代碼中,我相信它的類是:

System.Web.Mvc.UrlHelper

即命名空間是System.Web.Mvc。

因此,如果您真的在使用您在上面詳述的規格,就不能僅僅使用它,這是非常奇怪的。

+0

嚴重..沒有System.Web.Mvc.UrlHelper程序集,沒有這樣的名稱空間。 – 2010-10-12 11:07:01

+0

我沒有說過。我說在System.Web.Mvc命名空間中有一個名爲UrlHelper的類。 – awrigley 2010-10-12 11:17:18

+0

只是另一個錯誤:CS1012:太多的字符在字符文字,在同一行..很奇怪..( – 2010-10-12 11:33:35

2

亞歷克斯,

嘗試添加下面的擴展方法,看看它是否得到是你的任何進一步

public static partial class HtmlHelperExtensions 
{ 
    public static string Script(this HtmlHelper html, string path) 
    { 
     var filePath = VirtualPathUtility.ToAbsolute(path); 
     HttpContextBase context = html.ViewContext.HttpContext; 
     // don't add the file if it's already there 
     if (context.Items.Contains(filePath)) 
      return ""; 
     return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>"; 
    } 
} 

用法:

<%=Html.Script("~/Scripts/jquery-1.4.2.min.js")%> 

我知道這不會直接回答你的問題,但可以讓你移動正轉...

+0

吉姆,謝謝!這實際上是可以接受的..但我會試圖找出爲什麼Url.Content只是不存在???我現在使用谷歌搜索,無法找到它的文檔..也許它真的走了嗎? – 2010-10-12 11:08:51