从另一个TagHelper触发TagHelper [英] Trigger TagHelper from another TagHelper

查看:144
本文介绍了从另一个TagHelper触发TagHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想触发股票ScriptTagHelper(在GitHub上查看源代码),以便它可以模拟asp-append-version="true"属性.

I would like to trigger the stock ScriptTagHelper (view source on GitHub) so that it would emulate the asp-append-version="true" attribute.

我知道使用此功能的正确方法就是更改以下内容:

I know that the proper way to use this is to just change from this:

<script src="somefile.js"></script>

对此:

<script src="somefile.js" asp-append-version="true"></script>

此过程与CSS包含和图像(LinkTagHelperImageTagHelper)的版本控制非常相似.

This process is very similar for versioning CSS includes and images (LinkTagHelper and ImageTagHelper).

由于我有很多包含的脚本,样式表和图像,因此我想稍微自动化一下.因此,与其在每个HTML元素上都添加asp-append-version="true",不如创建一个自定义TagHelper来为我完成此任务.

Since I have a lot of included scripts, stylesheets, and images, I would like to automate things a bit. So instead of adding asp-append-version="true" on each and every HTML element, I would rather create a custom TagHelper that does this for me.

问题出在这里-它不起作用.

Herein lies the problem - it does not work.

当前,我的TagHelper仅覆盖script标签,看起来像这样:

Currently, my TagHelper covers only script tags and looks like this:

  [HtmlTargetElement("script", Attributes = "src")]      
  public class TestTagHelper : TagHelper
  {
    public override int Order => int.MinValue;
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
      if(!context.AllAttributes.ContainsName("asp-append-version"))
      {
        output.Attributes.SetAttribute("asp-append-version", "true");
      }
    }
  }

但不触发默认的ScriptTagHelper,而是直接输出 asp-append-version="true"到输出HTML.我还将Order属性设置为INT_MIN,以便它在任何其他Tag Helpers之前启动,但是仍然无法正常工作.

But instead of triggering the default ScriptTagHelper, it literally outputs the asp-append-version="true" to the output HTML. I have also set the Order property to INT_MIN, so that it fires before any other Tag Helpers, but it still doesn't work.

有没有办法使这项工作成功?

Is there a way to make this work?

推荐答案

如@ChrisPratt所述,无法链接TagHelpers.有一个肮脏的小技巧可以帮助您.您可以在自己的标签助手中手动创建ScriptTagHelper的实例,然后手动调用Process方法:

As @ChrisPratt mentioned, chaining TagHelpers is not possible. There is a little, dirty trick which might help you out. You could new up an instance of ScriptTagHelper manually in your own tag helper and invoke the Process method manually:

[HtmlTargetElement("script", Attributes = "src")]
public class TestTagHelper : TagHelper
{
    public override int Order => int.MinValue;

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        if (!context.AllAttributes.ContainsName("asp-append-version"))
        {
            var scriptTagHelper = new ScriptTagHelper(...) // Inject the required dependencies here
            {
                AppendVersion = true, // Explicitly set to true
                // Map all other properties
            };
            scriptTagHelper.Process(context, output);
        }
    }
}

这篇关于从另一个TagHelper触发TagHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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