如何创建从标准.Net Core脚本标记助手继承的脚本标记助手 [英] How to create a script tag helper that inherits from the standard .Net Core script tag helper

查看:89
本文介绍了如何创建从标准.Net Core脚本标记助手继承的脚本标记助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我维护着一个大型的遗留ASP.NET MVC应用程序,该应用程序最近已转换为.Net Core。

I maintain a large legacy ASP.NET MVC application, which was recently converted to .Net Core.

我需要为我们的JavaScript和CSS文件引入缓存清除功能。我赞赏使用新的.Net Core脚本标记帮助器上的 asp-append-version = true 属性可以轻松完成此操作。

I need to introduce cache busting for our JavaScript and CSS files. I appreciate this can easily be done using the asp-append-version="true" attribute on the new .Net Core script tag helper.

但是,我的应用程序在100多个位置中具有脚本标签。在所有这些位置添加该属性将涉及大量页面,这意味着需要进行大量回归测试。

However, my application has script tags in over a 100 places. Adding the attribute in all those places will touch large numbers of pages, which means a lot of regression testing.

是否有一种方法可以创建一个新的继承自脚本标记的助手从.Net Core脚本标签帮助程序中获取,并且该属性始终具有 asp-append-version = true 属性?

Is there a way to create a new script tag helper that inherits from the .Net Core script tag helper, and that always has the asp-append-version="true" attribute? That will give me cache busting without having to update lots of files.

推荐答案


... create一个从.Net Core脚本标记助手继承而来的新脚本标记助手,并且始终具有asp-append-version = true属性?

...create a new script tag helper that inherits from the .Net Core script tag helper, and that always has the asp-append-version="true" attribute?



代码(在GitHub上查看



Code (View on GitHub)

using System.Linq;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Caching.Memory;

namespace AspNetCoreScriptTagHelperOverride
{
    [HtmlTargetElement("script")] // A
    public class MyScriptTagHelper : ScriptTagHelper
    {
        public MyScriptTagHelper(
            IHostingEnvironment env,
            IMemoryCache cache,
            HtmlEncoder html,
            JavaScriptEncoder js,
            IUrlHelperFactory url) : base(env, cache, html, js, url) { } // B

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            const string appendVersion = "asp-append-version";
            if (!context.AllAttributes.Any(a => a.Name == appendVersion))
            {
                var attributes = new TagHelperAttributeList(context.AllAttributes);
                attributes.Add(appendVersion, true);
                context = new TagHelperContext(attributes, context.Items, context.UniqueId);
            } // E

            base.AppendVersion = true; // C
            base.Process(context, output); // D
        }
    }
}



说明




  • A:将 TagName 设置为脚本

  • B:实现基本构造函数。

  • C:硬代码 AppendVersion 为true。

  • D:调用基类的 Process

  • E :克服 AttributeMatcher.TryDetermineMode

  • Explanation

    • A: Set the TagName to "script".
    • B: Implement the base constructor.
    • C: Hard code AppendVersion to true.
    • D: Call the base class's Process.
    • E: Overcome AttributeMatcher.TryDetermineMode
    • _ViewImports.cshtml 删除现有的标签帮助程序并添加您的替代项

      @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
      @removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.ScriptTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers
      @addTagHelper *, AspNetCoreScriptTagHelperOverride
      

      Be确保使用您的程序集的名称。

      Be sure to use the name of your assembly.

      完成后,只要有脚本标记帮助器,您的代码就会执行。例如,下面两个都将 AppendVersion 设置为 true

      Once that is done, your code will execute wherever there is a script tag helper. For instance, both of the following will have AppendVersion set to true.

      <script src="~/js/site.js"></script>
      <script src="~/js/site.js" asp-append-version="true"></script>
      <script src="~/js/site.js" asp-append-version="false"></script>
      

      这将是结果HTML:

      <script src="/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0"></script>
      



      另请参见



      https://github.com/aspnet/Mvc/ blob / dev / src / Microsoft.AspNetCore.Mvc.TagHelpers / ScriptTagHelper.cs

      这篇关于如何创建从标准.Net Core脚本标记助手继承的脚本标记助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆