【问题标题】:Why can I not access UmbracoHelper in my own class为什么我无法在自己的班级中访问 UmbracoHelper
【发布时间】:2017-01-20 05:40:14
【问题描述】:

我正在尝试从我自己班级的 Umbraco 获取图像。

我不明白为什么我不能在课堂上使用@umbraco 助手。我在类中定义了它的命名空间,比如:

using Umbraco.Core.Models;
using Umbraco.Web;

我只能使用 UmbracoHelper,当我写的时候:

var umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

这是我的课:

using Umbraco.Core.Models;
using Umbraco.Web;

namespace House.Site.Helpers
{
    public static class ImageClass
    {
        public static string GetAbsoluteImageUrl(this IPublishedContent node, string propertyName)
        {
            var umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);

            var imgID = node.GetPropertyValue<string>("propertyName");
            var imgObject = umbracoHelper.TypedMedia(imgID);

            return imgObject.Url;
        }

    }
}

【问题讨论】:

    标签: c# asp.net-mvc umbraco umbraco7


    【解决方案1】:

    由于您的类没有从任何 Umbraco 基类继承,您必须自己实例化 UmbracoHelper。

    通过使用:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current)
    

    非常好,可以让你获得你需要的东西。

    您应该可以只使用UmbracoContext.Current 而不是完整的命名空间和类名,因为您已经拥有Umbraco.Webusing 语句。

    【讨论】:

      【解决方案2】:

      因为您需要一个助手实例才能使用它。你必须实例化它,因为它依赖于一个有效的 UmbracoContext,而 UmbracoContext 又需要一个有效的 HTTP 上下文。在您的视图中使用助手时,这已经为您处理好了,并且通过您的视图继承的类提供了一个实例。

      您可以在 Static references to web request instances (such as UmbracoHelper) 上的 Umbraco 文档中阅读更多详细信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-19
        • 2016-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-26
        相关资源
        最近更新 更多