2017-06-20 81 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

它的工作,谢谢! –