2017-01-19 58 views
1

我在Umbraco後臺存儲了一些電子郵件模板(指樣本,不要與umbraco模板混淆)。在某些時候,我需要使用此模板發送電子郵件(主題,正文,抄送,密送等)。爲此我使用自動生成的模型,這需要IPublishedContent進行初始化。如何從Umbraco API控制器獲取發佈的內容ASP.NET

所以我不能初始化這個模型,因爲我無法獲得類型IPUblishedContent的內容。

當我通過ID得到它一切正常。但我不想依賴id,因爲它會發生變化,並決定使用path而不能這樣做。我得到的NullReferenceException當我執行以下操作:

contentCache.GetByRoute(true, "relative path to email template")//I think I am mistaken here as I am using cache 

相對路徑看起來像"/settings/emailtemplate/registrationtemplate"任何人都可以點我如何實現這一點,因爲我是新來的一把umbraco並努力理解我應該怎麼做某些事情。


我最初嘗試另一種方法(這也是未成功)是獲得由文檔類型標識內容

var contentService = umbracoContext.Application.Services.ContentService; 
var contentTypeService = umbracoContext.Application.Services.ContentTypeService; 
var contentType = contentTypeService.GetContentType("emailSettings"); 
var templates = contentService.GetContentOfContentType(contentType.Id); 

然後我得到的內容從contentService的:

var content = templates.FirstOrDefault(c => c.Name.Contains("Registration")); 
var publishedContent = contentService.GetById(content.Id) 

但這裏我得到publishedContentIContent,並且無法將它轉換爲IPublishedContent


更新1

我發現原因是,我試圖得到一把umbraco API控制器發佈的內容。所以問題是有沒有辦法從API獲取已發佈的內容?

+0

什麼是你一把umbraco的版本? –

+0

它是Umbraco 7.5.7 –

+0

在GetByRoute部分,這是爲了通過它的URL獲取頁面。如果沒有帶有URL「/ settings/emailtemplate/registrationtemplate」的umbraco頁面,它將不會返回任何內容。 您的電子郵件模板是否有對應的Umbraco頁面,或iOS它只是一個獨立的模板? – Tim

回答

2

你沒有提到Umbraco,但你可以像這樣使用UmbracoHelper。

IPublishedContent content = UmbracoHelper.TypedContent(content.Id); 

如果你不能有需要訪問UmbracoHelper這樣的:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); 
+0

不幸的是它沒有解決這個問題。 'UmbracoHelper.TypedContent(content.Id);'空模型的結果。 –

+0

如果Id正確,並且內容節點位於umbraco發佈的內容緩存中,那麼您將獲得內容 - 如果內容不存在,ut將僅返回null,因此您要麼輸入了錯誤的id,要麼內容未發佈 –

+0

內容未發表。謝謝! –

相關問題