コード例 #1
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            output.CopyHtmlAttribute(SrcAttributeName, context);
            ProcessUrlAttribute(SrcAttributeName, output);

            var bareUrl = Src.Substring(0, Src.LastIndexOf('.'));

            if (AppendVersion)
            {
                EnsureFileVersionProvider();
            }

            foreach (var format in SourceFormats)
            {
                var formatUrl = $"{bareUrl}.{format}";
                var finalUrl  = AppendVersion
                    ? FileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.PathBase, formatUrl)
                    : formatUrl;
                output.Content.AppendHtml($@"<source type=""image/{format}"" srcset=""{finalUrl}"" />");
            }

            var url = AppendVersion
                ? FileVersionProvider.AddFileVersionToPath(ViewContext.HttpContext.Request.PathBase, Src)
                : Src;

            output.Content.AppendHtml(!Eager
                ? $@"<img src=""{url}"" alt=""{Alt}"" width=""{Width}"" height=""{Height}"">"
                : $@"<img src=""{url}"" alt=""{Alt}"" width=""{Width}"" height=""{Height}"" loading=""lazy"">");

            output.Attributes.Clear();
        }