2017-06-20 84 views
1

我試圖讓一把umbraco域在C#這樣的:一把umbraco - DocumentType場C#

(String.Format(Umbraco.Field("labelFailure").ToString(), username)); 

但我發現了以下錯誤:

Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end 
request. 

我不知道錯誤以及如何解決它。

謝謝!

+0

您試圖訪問Umbraco.Field在自定義的C#代碼,而不是在一個視圖? – Eyescream

+0

是的,我想製作一個ViewData。 –

回答

3

如果你不是在視圖中,你應該在IPublishedContent使用頁面的GetPropertyValue讓你的價值:

.. 
using Umbraco.Web; 
.. 

var idPage = 1234; // you should get this dynamically :) 
IPublishedContent page = Umbraco.TypedContent(idPage); 
var labelFailure = page.GetPropertyValue<string>("labelFailure"); 
+0

它的工作,謝謝! –