如何创建从标准.Net Core脚本标记助手继承的脚本标记助手

编程入门 行业动态 更新时间:2024-10-10 10:24:53
本文介绍了如何创建从标准.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>

      另请参见

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

更多推荐

如何创建从标准.Net Core脚本标记助手继承的脚本标记助手

本文发布于:2023-11-03 05:16:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554324.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   标记   助手   标准   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!