2011-08-24 27 views
0

我正在查看Url助手擴展方法上的article。我試圖按照這篇文章,但結果沒有任何書面擴展方法在佈局中顯示(共享* .chtml文件)。Url助手在佈局中不可見。 ASP.NET MVC 3

public static class UrlHelperExtensions 
{ 
    public static String Image(this System.Web.Mvc.UrlHelper helper, String fileName) 
    {    
     return helper.Content("~/Content/Images/" + fileName); 
    } 
    public static String Stylesheet(this System.Web.Mvc.UrlHelper helper, String fileName) 
    { 
     return helper.Content("~/Content/Stylesheets/" + fileName); 
    } 
    public static String Script(this System.Web.Mvc.UrlHelper helper, String fileName) 
    { 
     return helper.Content("~/Content/Scripts/" + fileName); 
    } 
} 

// inside the layout I tried to use this: 
<link href="@Url.Content("Site.css")" rel="stylesheet" type="text/css"/> 

如何在佈局中顯示Url擴展方法?

謝謝!

+0

你可以發佈你的代碼,顯示你的嘗試? –

+0

您的示例代碼在佈局文件中調用'Url.Content'。當你用'Url.Stylesheet()'替換它時會發生什麼?你的'UrlExtensions'類是什麼名字空間? –

回答

2

確保您有使用助手的位置的名稱空間的引用。

因此,如果您在MyClassLib.Web中創建了類UrlExtensions,請將@using MyClassLib.Web添加到.cshtml文件的頂部。