How to create a script tag helper that inherits from the standard .Net Core script tag helper

余生颓废 提交于 2019-12-01 11:56:05

...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?

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

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

Usage

In _ViewImports.cshtml remove the existing tag helper and add your override.

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

Be sure to use the name of your assembly.

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>

This will be the the resultant HTML:

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

See Also

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!