簡體   English   中英

為什么我不能在自己的課堂上訪問UmbracoHelper

[英]Why can I not access UmbracoHelper in my own class

我想在自己的課堂上從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;
        }

    }
}

由於您的類不是從任何Umbraco基類繼承,因此您必須自己實例化UmbracoHelper。

使用以下方式這樣做:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current)

非常好,可以讓你獲得你需要的東西。

您應該只能使用UmbracoContext.Current而不是完整的命名空間和類名,因為您已經擁有Umbraco.Webusing語句。

因為在使用之前需要幫助程序的實例。 您必須按照顯示的方式對其進行實例化,因為它依賴於有效的UmbracoContext,而UmbracoContext又需要有效的HTTP上下文。 在視圖中使用幫助程序時,已經為您處理了這個問題,並且通過您的視圖繼承的類提供了一個實例。

您可以在Umbraco文檔中關於Web請求實例的靜態引用(例如UmbracoHelper)中閱讀有關此內容的更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM