【问题标题】:AspNetCore get path to wwwroot in TagHelperAspNetCore 在 TagHelper 中获取 wwwroot 的路径
【发布时间】:2016-10-12 23:41:12
【问题描述】:

我尝试创建一个 TagHelper 来检查图像是否存在,如果不存在则替换默认图像的路径。

不幸的是,我在标签助手中映射“~”符号时遇到问题。

例如。我的图像 src 包含“~\images\image1.png”。现在我想检查这个文件是否存在,如果不从标签的属性中用另一个替换它。我坚持将“~”映射到我的应用程序的 wwwroot。

这是我实际拥有的:

[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);
        }
    }

}

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    您应该对 IUrlHelperFactor 和 IActionContextAccessor 使用构造函数依赖项,这将使您能够获得一个 IUrlHelper,该 IUrlHelper 可以使用“~/”语法从 wwwroot 解析 url

    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 中看到我正在这样做

    【讨论】:

    • 嗨..感谢您的帮助。我也已经尝试过这种方法。但是有了这个我得到“InvalidOperationException:在尝试激活'SeedCoreNetWeb.TagHelpers.ImageTagHelper'时无法解析类型'Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor'的服务。” :-(
    • 我解决了这个问题......谢谢......我会用我的完整代码创建另一个答案,以防有人想做同样的事情......
    【解决方案2】:

    需要在 Startup.cs 的 ConfigureServices 方法中添加

    您需要添加以下几行:

            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));
                        }
                    }
                }
            }
    
        }
    

    【讨论】:

    • 我在 Joe Audette 的帮助下解决了一个完整的代码。再次感谢。
    • services.AddSingleton(); services.AddSingleton();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    相关资源
    最近更新 更多