public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (Url == null) { return; } output.CopyHtmlAttribute(UrlAttributeName, context); Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext); UrlUtils.FromRelative(urlHelper.Content(Url), out PathString path, out QueryString query, out _); string url = null; var n = _bundleManagerFactory.Instances.Count; for (var i = 0; i < n; i++) { if ((url = await _bundleManagerFactory.Instances[i].TryGenerateUrlAsync(path, query, ViewContext.HttpContext)) != null) { break; } } if (url != null) { var index = output.Attributes.IndexOfName(UrlAttributeName); TagHelperAttribute existingAttribute = output.Attributes[index]; output.Attributes[index] = new TagHelperAttribute(existingAttribute.Name, url, existingAttribute.ValueStyle); } }
public static PathMap FromUrl(Microsoft.AspNetCore.Hosting.IHostingEnvironment env , Microsoft.AspNetCore.Mvc.IUrlHelper urlHelper , string baseSrc) { if (baseSrc.StartsWith("data:")) { throw new System.ArgumentException("data: cannot be transformed into URL."); } PathMap pmr = new PathMap(); pmr.Original = baseSrc; int argsPos = baseSrc.IndexOf("?"); int hashPos = baseSrc.IndexOf("#"); pmr.QueryString = ""; pmr.Hash = ""; if (hashPos != -1) { pmr.Hash = baseSrc.Substring(hashPos); baseSrc = baseSrc.Substring(0, hashPos); } if (argsPos != -1) { pmr.QueryString = baseSrc.Substring(argsPos); baseSrc = baseSrc.Substring(0, argsPos); } string imageVirtualPath = null; if ( baseSrc.StartsWith("http:", System.StringComparison.InvariantCultureIgnoreCase) || baseSrc.StartsWith("https:", System.StringComparison.InvariantCultureIgnoreCase)) { pmr.Canonical = baseSrc; string domain = urlHelper.ActionContext.HttpContext.Request.Scheme + "://" + urlHelper.ActionContext.HttpContext.Request.Host.Value; if (baseSrc.StartsWith(domain, System.StringComparison.InvariantCultureIgnoreCase)) { baseSrc = baseSrc.Substring(domain.Length); } else { pmr.PhysicalApplicable = false; System.Uri uri = new System.Uri(baseSrc, System.UriKind.Absolute); pmr.Absolute = uri.AbsolutePath; } } if (baseSrc.StartsWith("~")) { imageVirtualPath = urlHelper.Content(baseSrc); // To Virtual Path } else if (baseSrc.StartsWith("/")) { imageVirtualPath = baseSrc; } else if ( !baseSrc.StartsWith("http:", System.StringComparison.InvariantCultureIgnoreCase) && !baseSrc.StartsWith("https:", System.StringComparison.InvariantCultureIgnoreCase) ) { imageVirtualPath = urlHelper.ActionContext.HttpContext.Request.PathBase.Value; imageVirtualPath += urlHelper.ActionContext.HttpContext.Request.Path.Value; if (!string.IsNullOrEmpty(imageVirtualPath)) { if (!imageVirtualPath.EndsWith("/")) { int li = imageVirtualPath.LastIndexOf('/'); if (li != -1) { imageVirtualPath = imageVirtualPath.Substring(0, li + 1); } System.Console.WriteLine(imageVirtualPath); } //if (!string.IsNullOrEmpty(imageVirtualPath)) // imageVirtualPath += "/"; } imageVirtualPath += baseSrc; } pmr.Absolute = imageVirtualPath; if (pmr.Canonical == null) { pmr.Canonical = urlHelper.ActionContext.HttpContext.Request.Scheme + "://" + urlHelper.ActionContext.HttpContext.Request.Host.Value; pmr.Canonical += pmr.Absolute; // System.Uri bas = new System.Uri(HttpContext.Request.Scheme + "://" + HttpContext.Request.Host.Value, System.UriKind.Absolute); // System.Uri canonicalUrl = new System.Uri(bas, imagePhysicalPath); } if (pmr.PhysicalApplicable && env != null) { string rootPath = urlHelper.Content("~"); // To Virtual Path if (imageVirtualPath.StartsWith(rootPath, System.StringComparison.InvariantCultureIgnoreCase)) { pmr.Physical = imageVirtualPath.Substring(rootPath.Length); } else { pmr.Physical = imageVirtualPath; } if (pmr.Physical.StartsWith('/')) { pmr.Physical = pmr.Physical.Substring(1); } // https://stackoverflow.com/questions/40001242/aspnetcore-get-path-to-wwwroot-in-taghelper // this.HostingEnvironment.WebRootPath // d.h: /wwwroot // this.HostingEnvironment.ContentRootPath // d.h: /wwwroot/.. pmr.Physical = pmr.Physical.Replace('/', System.IO.Path.DirectorySeparatorChar); pmr.Physical = System.IO.Path.Combine(env.WebRootPath, pmr.Physical); pmr.Physical = System.IO.Path.GetFullPath(pmr.Physical); } return(pmr); }