Пример #1
0
        // https://stackoverflow.com/questions/40001242/aspnetcore-get-path-to-wwwroot-in-taghelper
        public override void Process(Microsoft.AspNetCore.Razor.TagHelpers.TagHelperContext context
                                     , Microsoft.AspNetCore.Razor.TagHelpers.TagHelperOutput output)
        {
            EnsureFileVersionProvider();


            Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper =
                base.UrlHelperFactory.GetUrlHelper(this.ActionContext);

            // string myUrl = urlHelper.Content("~/somefilebelowwwwroot");



            // string imgPath = output.Attributes["src"].Value as string;
            // Microsoft.AspNetCore.Html.HtmlString hs = output.Attributes["src"].Value as Microsoft.AspNetCore.Html.HtmlString;
            // string imgPath = hs.Value;

            // Microsoft.AspNetCore.Html.HtmlString hs = context.AllAttributes["src"].Value as Microsoft.AspNetCore.Html.HtmlString;
            // string imgPath = hs.Value;

            // var rcc = new Microsoft.AspNetCore.Mvc.Routing.UrlRouteContext();
            // var rc = new Microsoft.AspNetCore.Routing.RouteContext(this.HttpContext);

            // lol = urlHelper.RouteUrl(rcc);



            string baseSrc = context.AllAttributes["src"].Value.ToString();

            if (baseSrc.StartsWith("data:"))
            {
                return;
            }

            PathMap pm = PathMap.FromUrl(this.HostingEnvironment, urlHelper, baseSrc);

            System.IO.FileInfo fi = new System.IO.FileInfo(pm.Physical);

            string unixTicks = fi.Exists ? fi.LastWriteTimeUtc.ToUnixTicksString()
                : System.DateTime.UtcNow.ToUnixTicksString();

            if (string.IsNullOrEmpty(pm.QueryString))
            {
                baseSrc = $"{pm.Absolute}?no_cache={unixTicks}{pm.Hash}";
            }
            else
            {
                baseSrc = $"{pm.Absolute}{pm.QueryString}&no_cache={unixTicks}{pm.Hash}";
            }

            // No not have it XML-Attribute-encoded
            //output.Attributes.SetAttribute("src", new Microsoft.AspNetCore.Html.HtmlString(imgPath));
            output.Attributes.SetAttribute("src", baseSrc);

            //output.Attributes.SetAttribute("src", src + "?&v=123");
            // output.Attributes.SetAttribute(SrcAttributeName, _fileVersionProvider.AddFileVersionToPath(Src));
        }