首页 > 其他分享 >Sitecore MVC @Html.RenderSection() 的替代方案

Sitecore MVC @Html.RenderSection() 的替代方案

时间:2023-11-11 22:00:12浏览次数:34  
标签:RenderSection string helper storage Html MVC context identifier

source

在 Sitecore 里的 MVC 没有 @Html.RenderSection,所以使用以下的一个替代方案:

public static class HtmlRenderExtensions
{
    /// <summary>
    /// Delegate script/resource/etc injection until the end of the page
    /// <para>@via https://stackoverflow.com/a/14127332/1037948 and http://jadnb.wordpress.com/2011/02/16/rendering-scripts-from-partial-views-at-the-end-in-mvc/ </para>
    /// </summary>
    private class DelayedInjectionBlock : IDisposable
    {
        /// <summary>
        /// Unique internal storage key
        /// </summary>
        private const string CACHE_KEY = "DCCF8C78-2E36-4567-B0CF-FE052ACCE309"; // "DelayedInjectionBlocks";

        /// <summary>
        /// Internal storage identifier for remembering unique/isOnlyOne items
        /// </summary>
        private const string UNIQUE_IDENTIFIER_KEY = CACHE_KEY;

        /// <summary>
        /// What to use as internal storage identifier if no identifier provided (since we can't use null as key)
        /// </summary>
        private const string EMPTY_IDENTIFIER = "";

        /// <summary>
        /// Retrieve a context-aware list of cached output delegates from the given helper; uses the helper's context rather than singleton HttpContext.Current.Items
        /// </summary>
        /// <param name="helper">the helper from which we use the context</param>
        /// <param name="identifier">optional unique sub-identifier for a given injection block</param>
        /// <returns>list of delayed-execution callbacks to render internal content</returns>
        public static Queue<string> GetQueue(HtmlHelper helper, string identifier = null)
        {
            return _GetOrSet(helper, new Queue<string>(), identifier ?? EMPTY_IDENTIFIER);
        }

        /// <summary>
        /// Retrieve a context-aware list of cached output delegates from the given helper; uses the helper's context rather than singleton HttpContext.Current.Items
        /// </summary>
        /// <param name="helper">the helper from which we use the context</param>
        /// <param name="defaultValue">the default value to return if the cached item isn't found or isn't the expected type; can also be used to set with an arbitrary value</param>
        /// <param name="identifier">optional unique sub-identifier for a given injection block</param>
        /// <returns>list of delayed-execution callbacks to render internal content</returns>
        private static T _GetOrSet<T>(HtmlHelper helper, T defaultValue, string identifier = EMPTY_IDENTIFIER) where T : class
        {
            var storage = GetStorage(helper);

            // return the stored item, or set it if it does not exist
            return (T)(storage.ContainsKey(identifier) ? storage[identifier] : (storage[identifier] = defaultValue));
        }

        /// <summary>
        /// Get the storage, but if it doesn't exist or isn't the expected type, then create a new "bucket"
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetStorage(HtmlHelper helper)
        {
            var storage = helper.ViewContext.HttpContext.Items[CACHE_KEY] as Dictionary<string, object>;
            if (storage == null) helper.ViewContext.HttpContext.Items[CACHE_KEY] = (storage = new Dictionary<string, object>());
            return storage;
        }


        private readonly HtmlHelper helper;
        private readonly string identifier;
        private readonly string isOnlyOne;

        /// <summary>
        /// Create a new using block from the given helper (used for trapping appropriate context)
        /// </summary>
        /// <param name="helper">the helper from which we use the context</param>
        /// <param name="identifier">optional unique identifier to specify one or many injection blocks</param>
        /// <param name="isOnlyOne">extra identifier used to ensure that this item is only added once; if provided, content should only appear once in the page (i.e. only the first block called for this identifier is used)</param>
        public DelayedInjectionBlock(HtmlHelper helper, string identifier = null, string isOnlyOne = null)
        {
            this.helper = helper;

            // start a new writing context
            ((WebViewPage)this.helper.ViewDataContainer).OutputStack.Push(new StringWriter());

            this.identifier = identifier ?? EMPTY_IDENTIFIER;
            this.isOnlyOne = isOnlyOne;
        }

        /// <summary>
        /// Append the internal content to the context's cached list of output delegates
        /// </summary>
        public void Dispose()
        {
            // render the internal content of the injection block helper
            // make sure to pop from the stack rather than just render from the Writer
            // so it will remove it from regular rendering
            var content = ((WebViewPage)this.helper.ViewDataContainer).OutputStack;
            var renderedContent = content.Count == 0 ? string.Empty : content.Pop().ToString();

            // if we only want one, remove the existing
            var queue = GetQueue(this.helper, this.identifier);

            // get the index of the existing item from the alternate storage
            var existingIdentifiers = _GetOrSet(this.helper, new Dictionary<string, int>(), UNIQUE_IDENTIFIER_KEY);

            // only save the result if this isn't meant to be unique, or
            // if it's supposed to be unique and we haven't encountered this identifier before
            if (null == this.isOnlyOne || !existingIdentifiers.ContainsKey(this.isOnlyOne))
            {
                // remove the new writing context we created for this block
                // and save the output to the queue for later
                queue.Enqueue(renderedContent);

                // only remember this if supposed to
                if (null != this.isOnlyOne) existingIdentifiers[this.isOnlyOne] = queue.Count; // save the index, so we could remove it directly (if we want to use the last instance of the block rather than the first)
            }
        }
    }


    /// <summary>
    /// <para>Start a delayed-execution block of output -- this will be rendered/printed on the next call to <see cref="RenderDelayed"/>.</para>
    /// <para>
    /// <example>
    /// Print once in "default block" (usually rendered at end via <code>@Html.RenderDelayed()</code>).  Code:
    /// <code>
    /// @using (Html.Delayed()) {
    ///     <b>show at later</b>
    ///     <span>@Model.Name</span>
    ///     etc
    /// }
    /// </code>
    /// </example>
    /// </para>
    /// <para>
    /// <example>
    /// Print once (i.e. if within a looped partial), using identified block via <code>@Html.RenderDelayed("one-time")</code>.  Code:
    /// <code>
    /// @using (Html.Delayed("one-time", isOnlyOne: "one-time")) {
    ///     <b>show me once</b>
    ///     <span>@Model.First().Value</span>
    /// }
    /// </code>
    /// </example>
    /// </para>
    /// </summary>
    /// <param name="helper">the helper from which we use the context</param>
    /// <param name="injectionBlockId">optional unique identifier to specify one or many injection blocks</param>
    /// <param name="isOnlyOne">extra identifier used to ensure that this item is only added once; if provided, content should only appear once in the page (i.e. only the first block called for this identifier is used)</param>
    /// <returns>using block to wrap delayed output</returns>
    public static IDisposable Delayed(this HtmlHelper helper, string injectionBlockId = null, string isOnlyOne = null)
    {
        return new DelayedInjectionBlock(helper, injectionBlockId, isOnlyOne);
    }

    /// <summary>
    /// Render all queued output blocks injected via <see cref="Delayed"/>.
    /// <para>
    /// <example>
    /// Print all delayed blocks using default identifier (i.e. not provided)
    /// <code>
    /// @using (Html.Delayed()) {
    ///     <b>show me later</b>
    ///     <span>@Model.Name</span>
    ///     etc
    /// }
    /// </code>
    /// -- then later --
    /// <code>
    /// @using (Html.Delayed()) {
    ///     <b>more for later</b>
    ///     etc
    /// }
    /// </code>
    /// -- then later --
    /// <code>
    /// @Html.RenderDelayed() // will print both delayed blocks
    /// </code>
    /// </example>
    /// </para>
    /// <para>
    /// <example>
    /// Allow multiple repetitions of rendered blocks, using same <code>@Html.Delayed()...</code> as before.  Code:
    /// <code>
    /// @Html.RenderDelayed(removeAfterRendering: false); /* will print */
    /// @Html.RenderDelayed() /* will print again because not removed before */
    /// </code>
    /// </example>
    /// </para>
    /// </summary>
    /// <param name="helper">the helper from which we use the context</param>
    /// <param name="injectionBlockId">optional unique identifier to specify one or many injection blocks</param>
    /// <param name="removeAfterRendering">only render this once</param>
    /// <returns>rendered output content</returns>
    public static MvcHtmlString RenderDelayed(this HtmlHelper helper, string injectionBlockId = null, bool removeAfterRendering = true)
    {
        var stack = DelayedInjectionBlock.GetQueue(helper, injectionBlockId);

        var startFlag = string.Format("<!-- delayed-block: {0} -->", injectionBlockId);
        var endFlag = string.Format("<!-- delayed-block: {0} - END -->", injectionBlockId);
        if (removeAfterRendering)
        {
            var sb = new StringBuilder(startFlag);
            // .count faster than .any
            while (stack.Count > 0)
            {
                sb.AppendLine(stack.Dequeue());
            }

            sb.Append(endFlag);

            return MvcHtmlString.Create(sb.ToString());
        }

        return MvcHtmlString.Create(startFlag + string.Join(Environment.NewLine, stack) + endFlag);
    }
}

使用方法:

// ~/Shared/_Layout.cshtml
@Html.RenderDelayed("header");

// ~/Views/Home/Index.cshtml
@using (Html.Delayed("header"))
{
    <script>
    console.log("delayed.");
    </script>
}

标签:RenderSection,string,helper,storage,Html,MVC,context,identifier
From: https://www.cnblogs.com/fires/p/17826436.html

相关文章

  • 如何避免HTML iframe导致页面刷新
    要避免HTMLiframe导致页面刷新,您可以采取以下几种方法:使用AJAX加载内容:使用JavaScript的AJAX技术来异步加载iframe中的内容,这样就可以避免整个页面的刷新。您可以使用XMLHttpRequest或者jQuery的$.ajax方法来实现异步加载。设置iframe的sandbox属性:将iframe的sandbox属性设置为"al......
  • Java基础、MySQL数据库、Web前端(HTML、CSS、JavaScript)
    一、选择题(每题1分,共20题,共20分),注:可能有多选哦!1、在Java中,下列标识符不合法的有(   )A.newB.$UsdollarsC.1234 D.car.taxi2、定义了int型二维数组int[][]a=newinta[6][7]后,数组元素a[3][4]前的数组元素个数为(  )  A.24 B.25 C.18 D.173、下面程序的运行结果是( ......
  • 12、SpringMVC之拦截器
    12.1、环境搭建创建名为spring_mvc_interceptor的新module,过程参考9.1节和9.5节12.1.1、页面请求示例<ath:href="@{/test/hello}">测试拦截器</a>12.1.2、控制器方法示例@RequestMapping("/test/hello")publicStringtestHello(){return"succe......
  • SprigMvc文件下载
    @RequestMapping(method=RequestMethod.GET,value="/down")publicResponseEntity<byte[]>DownLoad(HttpServletRequestrequest,Stringfilename){//获取文件的真实路径StringrealPath=request.getServletContext().getRealPath("/WEB-INF/d......
  • ASP.NET MVC 身份认证(筛选器的使用)
    我们在做一些后台项目时,不希望用户未进行登录就轻易登录后台页面进行操作,那么这里简单示范一个身份认证的方法新建一个普通的.cs类这个类继承在  ActionFilterAttributeusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.......
  • Html 表格 在线转 Markdown
    复制HTMLTableF12查看网页源代码Htmltomarkdown在线转换https://tableconvert.com/html-to-markdown复制Markdown还有好多其它的在线转换功能,非常不错......
  • 「Java开发指南」如何用MyEclipse搭建Spring MVC应用程序?(二)
    本教程将指导开发者如何生成一个可运行的SpringMVC客户应用程序,该应用程序实现域模型的CRUD应用程序模式。在本教程中,您将学习如何:从数据库表的Scaffold到现有项目部署搭建的应用程序在上文中,我们介绍了如何创建一个Web项目和来自数据库表的Scaffold等,本文将继续介绍如何部......
  • html例子
    HTML部分<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-e......
  • 前端学习之html和css
    前端    快速入门篇:首先给个title,就是页面标题,比如说这页的博客后台 - 博客园这种,第二就是我在它的页面身体里面的标题有h1——h6六种大小的标题,可以按需选择,这个是文字类,然后为了美观一点,可以放图片上去,然后图片也得有它的大小尺寸,包括文字标题也是对吧,所以就引入......
  • Taurus .Net Core 微服务开源框架:Admin 插件【4-4】 - 配置管理-Mvc【Plugin-CORS 跨
    前言:继上篇:Taurus.NetCore微服务开源框架:Admin插件【4-3】-配置管理-Mvc【Plugin-MicroService微服务】本篇继续介绍下一个内容:系统配置节点:Mvc- Plugin- CORS 跨域界面:界面如下:跨域功能相关配置说明如下:1、CORS.IsEnable:是否启用跨域功能。仅需要开启该功......