2017-01-20 102 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)上閱讀關於此的更多詳細信息。

相關問題