AspNetCore gets wwwroot path in TagHelper

I am trying to create a TagHelper that checks for the presence of an image, and if it does not replace the default image path.

Unfortunately, I have problems displaying the "~" character in my tag helper.

For instance. My image src contains "~ \ images \ image1.png". Now I want to check the existence of this file and, if not replace it with another, from the tag attribute. I am stuck matching "~" with wwwroot of my application.

Here is what I actually do:

[HtmlTargetElement("img", TagStructure = TagStructure.WithoutEndTag)]
public class ImageTagHelper : TagHelper
{
    public ImageTagHelper(IHostingEnvironment environment)
    {
        this._env = environment;
    }

    private IHostingEnvironment _env;

    public string DefaultImageSrc { get; set; }

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
    //    urlHelper.ActionContext.HttpContext.
    //var env = ViewContext.HttpContext.ApplicationServices.GetService(typeof(IHostingEnvironment)) as IHostingEnvironment;

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

        if (!File.Exists(_env.WebRootPath + imgPath)) {
            output.Attributes.SetAttribute("src", _env.WebRootPath + DefaultImageSrc);
        }
    }

}
+4
source share
2 answers

IUrlHelperFactor IActionContextAccessor, IUrlHelper, URL- wwwroot ~/"

public ImageTagHelper(
    IUrlHelperFactory urlHelperFactory,
    IActionContextAccessor actionContextAccesor,)
{
    this.urlHelperFactory = urlHelperFactory;
    this.actionContextAccesor = actionContextAccesor;
}

private IUrlHelperFactory urlHelperFactory;
private IActionContextAccessor actionContextAccesor;

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    var urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccesor.ActionContext);
    var myUrl = urlHelper.Content("~/somefilebelowwwwroot");
    ...
}

, PagerTagHelper

+4

ConfigureServices Startup.cs

:

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

:

   [HtmlTargetElement("img", TagStructure = TagStructure.WithoutEndTag)]
    public class ImageTagHelper : TagHelper
    {
        public ImageTagHelper(IUrlHelperFactory urlHelperFactory,
                              IActionContextAccessor actionContextAccessor,
                              IHostingEnvironment environment)
        {
            _urlHelperFactory = urlHelperFactory;
            _actionContextAccessor = actionContextAccessor;
            _env = environment;
        }

        private IUrlHelperFactory _urlHelperFactory;
        private IActionContextAccessor _actionContextAccessor;

        private IHostingEnvironment _env;

        public string DefaultImageSrc { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            string imgPath = context.AllAttributes["src"].Value.ToString();

            IUrlHelper urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);

            if (!imgPath.StartsWith("data:"))
            {
                if (!File.Exists(_env.WebRootPath + urlHelper.Content(imgPath)))
                {
                    if (DefaultImageSrc != null)
                    {
                        output.Attributes.SetAttribute("src", urlHelper.Content(DefaultImageSrc));
                    }
                }
            }
        }

    }
+3

Source: https://habr.com/ru/post/1657509/


All Articles