public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = "img"; output.TagMode = TagMode.SelfClosing; HttpClient httpClient = new HttpClient(); string fileUrl = ""; if (Src.StartsWith("http") || FallbackUrl.StartsWith("https")) { var webUrl = new Uri(Src); httpClient.BaseAddress = new Uri($"{webUrl.Scheme}://{webUrl.Host}"); Src = webUrl.LocalPath; } else { var baseUrl = httpContextAccessor.HttpContext.Request.GetDisplayUrl(); httpClient.BaseAddress = new Uri(baseUrl); } using (HttpResponseMessage response = await httpClient.GetAsync(Src)) { if (response.IsSuccessStatusCode) { output.Attributes.SetAttribute("src", Src); } else { output.Attributes.SetAttribute("src", FallbackUrl); } } }
protected override void Render(HtmlTextWriter writer) { if (Src.StartsWith("~")) { Src = base.ResolveUrl(Src); } else if (Src.StartsWith("/")) { Src = GetImagePath() + Src; } else { Src = GetImagePath() + "/" + Src; } writer.Write(string.Format(CultureInfo.InvariantCulture, imageFormat, new object[] { Src })); }