【问题标题】:How to implement asp-append-version="true" to background-image property?如何实现 asp-append-version="true" 到背景图像属性?
【发布时间】:2021-02-15 09:58:28
【问题描述】:

我正在尝试对我的图像实施 HTMLTagHelper asp-append-version="true"。 问题在于 DOM 分布,我没有将属性分配给 <img> 标记,而是分配给包含具有 background-url 属性的图像的 <div>

而且,div 是在所有 DOM 加载之前生成的,我不知道是否会有不同的方法。

很明显,将div 更改为img 标记,但我不想要它,因为我的设计必须保持不变。

我的 javascript 一直是这样的:

cardHTML += '<div asp-append-version="true" class="card littleCard" style="background-image: url(/Content/Img/Especialistas/LittleCard/' + especialista.idEspecialista + '.jpg' + ')' + '" >';
cardHTML += '</div>';

asp-append-version="true" 不适用于 div 标记。 关于如何找到处理此问题的方法的任何想法?

谢谢

【问题讨论】:

标签: c# html asp.net-core asp.net-core-tag-helpers asp-append-version


【解决方案1】:

您可以创建自定义TagHelper 来定位具有内联样式属性的所有元素。我尝试过的以下示例看起来工作正常,但如果您想要更标准的东西(类似于ImageTagHelper,...),您可以尝试查看基类UrlResolutionTagHelper。我不太确定为什么它需要更复杂,基本上你需要在实际处理它之前解析 URL。我尝试了一个简单的IFileVersionProvider,它也适用于相对路径(当然解析的路径应该在当前服务器的网络根目录)。

下面这个简单的例子对于HtmlString的属性值很好(这几乎是常见的情况,一些自定义渲染可能会注入IHtmlContent而不是HtmlString,对于这种复杂的情况,你可以参考UrlResolutionTagHelper 的源代码,即使复制几乎完全相同的相关代码也可以):

//target only elements having an inline style attribute
[HtmlTargetElement(Attributes = "style")]
public class InlineStyleBackgroundElementTagHelper : TagHelper
{
    readonly IFileVersionProvider _fileVersionProvider;        
    const string BACKGROUND_URL_PATTERN = "(background(?:-image)?\\s*:[^;]*url)(\\([^)]+\\))";
    public InlineStyleBackgroundElementTagHelper(IFileVersionProvider fileVersionProvider)
    {
        _fileVersionProvider = fileVersionProvider;
    }
    //bind the asp-append-version property
    [HtmlAttributeName("asp-append-version")]
    public bool AppendsVersion { get; set; }
    //inject ViewContext from the current request
    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        if (AppendsVersion)
        {
            if (output.Attributes.TryGetAttribute("style", out var styleAttr))
            {
                //the value here should be an HtmlString, so this basically 
                //gets the raw plain string of the style attribute's value
                var inlineStyle = styleAttr.Value.ToString();
                var basePath = ViewContext.HttpContext.Request.PathBase;
                inlineStyle = Regex.Replace(inlineStyle, BACKGROUND_URL_PATTERN, m =>
                {
                    //extract the background url contained in the inline style
                    var backgroundUrl = m.Groups[2].Value.Trim('(', ')', ' ');
                    //append the version
                    var versionedUrl = _fileVersionProvider.AddFileVersionToPath(basePath, backgroundUrl);

                    //format back the inline style with the versioned url
                    return $"{m.Groups[1]}({versionedUrl})";
                }, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                output.Attributes.SetAttribute("style", inlineStyle);
            }
        }
    }
}

用法:就像你在其他内置标签上使用asp-append-version 一样有帮助。 (就像你的例子一样)。

【讨论】:

  • 你好。我实际上是在尝试首先获取视图上下文,因此继续将 URL 从我的助手转换为控制器。让我们看看这是否可行。非常感谢
  • @OrisSin 我在这里测试了代码,它应该适用于您的情况(将文件版本附加到 any 的样式属性中指定的背景 url b> 元素,而不仅仅是div)。如果您有任何问题,请发表评论或更新您的问题并告诉我,我可能会帮助您,直到您可以采用此解决方案。
猜你喜欢
  • 1970-01-01
  • 2023-01-30
  • 2016-04-25
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 2016-01-08
相关资源
最近更新 更多