2015-12-02 105 views
1

我正在使用ASP.NET 5/MVC 6.我在部分頁面內使用自定義HtmlHelper來註冊帶有HttpContext的腳本標記,以便我可以延遲呈現腳本標記直到頁面的結尾。HtmlHelper實現MVC 6 vNext

此代碼註冊腳本標籤:

public static HtmlString AddResource(this IHtmlHelper HtmlHelper, PageResourceType resourceType, Func<object, HelperResult> Template) 
    {  
      if (HtmlHelper.ViewContext.HttpContext.Items[resourceType] != null) 
      { 
       ((List<Func<object, HelperResult>>)HtmlHelper.ViewContext.HttpContext.Items[resourceType]).Add(Template); 
      } 
      else 
      { 
       HtmlHelper.ViewContext.HttpContext.Items[resourceType] = new List<Func<object, HelperResult>>() { Template }; 
      } 

      return new HtmlString(String.Empty); 
    } 

從使用這種輔助剃刀頁面註冊腳本標籤的使用是

@Html.AddResource(PageResourceType.JavaScript , 
        @<script src='~/js/plugin/sparkline/jquery.sparkline.js'></script> 
        ) 

此代碼,從_Layout.cshtml叫,呈現任何已註冊的腳本標籤:

public static HtmlString RenderResources(this IHtmlHelper HtmlHelper, PageResourceType resourceType) 
    { 
     if (HtmlHelper.ViewContext.HttpContext.Items[resourceType] != null) 
     { 
      List<Func<object, HelperResult>> Resources = (List<Func<object, HelperResult>>)HtmlHelper.ViewContext.HttpContext.Items[resourceType]; 

      foreach (var Resource in Resources) 
      { 
       if (Resource != null) 
       { 
        HtmlHelper.ViewContext.Writer.Write(Resource(null)); 
       } 
      } 
     } 

     return new HtmlString(String.Empty); 
    } 

這兩位很好地工作,但是,我n ow有一個額外的HtmlHelper,我想用它來創建一個依賴腳本標記的特定UI元素(在這種情況下爲Sparkline UI小部件)。

public static HtmlString Sparkline(this IHtmlHelper HtmlHelper, IEnumerable<int> values, string cssClasses) 
    {    
     StringBuilder sb = new StringBuilder(); 
     sb.Append("<div class='sparkline txt-color-blue hidden-mobile hidden-md hidden-sm'>"); 
     sb.Append(string.Join(",", values)); 
     sb.Append("</div>"); 

     HtmlHelper.AddResource(PageResourceType.JavaScript, (x) => 
     { 
      return new HelperResult(writer => 
      { 
       string script = "<script src='~/js/plugin/sparkline/jquery.sparkline.js'></script>"; 
       return new Task(() => writer.Write(script)); 
      }); 
     }); 

     return new HtmlString(sb.ToString()); 
    } 

注意,這使得通過

HtmlHelper.AddResource 

問題以前的寄存器方法的調用是,RenderResources代碼執行時,應用程序只是掛起下去。我在舊的MVC 5系統下工作,但必須對新的MVC 6結構進行一些更改(標籤助手是什麼導致了我認爲的變化),並且我以某種方式搗亂了它。

任何想法如何正確實施Sparkline方法,特別是對HtmlHelper.AddResource的調用?

TIA!

回答

0

當然,我發佈問題的那一刻,我找到了答案。雖然,我不知道爲什麼它解決了這個問題,因爲我認爲包裝任務內的.WRITE方法是與調用.WriteAsync ...

HtmlHelper.AddResource(PageResourceType.JavaScript, (x) => 
    { 
     return new HelperResult(writer => 
     { 
      string script = "<script src='~/js/plugin/sparkline/jquery.sparkline.js'></script>"; 
      return new Task(() => writer.Write(script)); 
     }); 
    }); 

應該

HtmlHelper.AddResource(PageResourceType.JavaScript, (x) => 
    { 
     return new HelperResult(writer => 
     { 
      string script = "<script src='~/js/plugin/sparkline/jquery.sparkline.js'></script>"; 
      return writer.WriteAsync(script); 
     }); 
    });