2017-01-20 177 views
2

我想在我自己的课堂中从Umbraco中获得图像。为什么我不能在自己的课堂上使用UmbracoHelper

我不明白为什么我不能在班上使用@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; 
     } 

    } 
} 

回答

9

由于您的类不是从任何Umbraco基类继承,你将不得不自己实例化UmbracoHelper。

否则使用:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current) 

是完全正常的,并会得到你获得你所需要的东西。

您应该只能使用UmbracoContext.Current而不是完整的名称空间和类名称,因为您已有using语句Umbraco.Web

0

因为您需要帮助器的实例才能使用它。您必须按照您的示例来实例化它,因为它取决于有效的UmbracoContext,而UmbracoContext需要有效的HTTP上下文。在视图中使用助手时,已经为您和您的视图继承的类提供了一个实例。

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

相关问题