2009-06-14 83 views
3

我正在爲MVC Web應用程序中的HtmlHelper類創建擴展方法。什麼都沒有顯示,甚至沒有默認的InputExtensions。擴展方法未顯示

public static class HtmlHelpers 
{ 
    public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script) 
    { 
     if (!RegisteredScriptIncludes.ContainsValue(script)) 
     { 
      RegisteredScriptIncludes.Add(RegisteredScriptIncludes.Count, script); 
     } 
    } 

    public static string RenderScripts(this HtmlHelper htmlhelper) 
    { 
     var scripts = new StringBuilder(); 
     foreach (string script in RegisteredScriptIncludes.Values) 
     { 
      scripts.AppendLine("<script src='" + script + "' type='text/javascript'></script>"); 
     } 
     return scripts.ToString(); 

    } 

    private static SortedList<int, string> RegisteredScriptIncludes 
    { 
     get 
     { 
      SortedList<int, string> value = (SortedList<int, string>)HttpContext.Current.Items["RegisteredScriptIncludes"]; 
      if (value == null) 
      { 
       value = new SortedList<int, string>(); 
       HttpContext.Current.Items["RegisteredScriptIncludes"] = value; 
      } 
      return value; 
     } 
    } 

} 

擴展方法沒有顯示在代碼中。

他們在哪裏?

回答

8

您忘記了using聲明嗎?具體而言,您需要「using path.to.my.namespace;」來獲取擴展方法。

3

另一個顯而易見的事情是,如果有人發現這篇文章,在您的擴展方法的第一個參數中忘記了「this」關鍵字。如果你忘記了這一點,編譯器可以幫助你的東西並不多!