HtmlHelper实现MVC 6 vNext

时间:2015-12-02 18:52:24

标签: asp.net-mvc razor asp.net-core asp.net-core-mvc tag-helpers

我正在使用ASP.NET 5 / MVC 6.我在部分页面内使用自定义HtmlHelper来注册带有HttpContext的脚本标记,这样我就可以延迟呈现脚本标记,直到结束这页纸。

此代码注册脚本标记:

    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);
    }

使用此帮助程序从Razor页面注册脚本标记的用法是

 @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);
    }

这两个位很好用,但是,我现在有一个额外的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结构(Tag Helpers导致了我认为的改变),并且我以某种方式搞砸了它。

知道如何正确实现Sparkline方法,尤其是对HtmlHelper.AddResource的调用吗?

TIA!

1 个答案:

答案 0 :(得分:0)

当然,在我发布问题的那一刻,我找到了答案。虽然,我不确定为什么它解决了这个问题,因为我认为将.Write方法包装在一个Task中与调用.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);
        });
    });