在 Umbraco 页面中渲染内容

Rendering content inside an Umbraco page

我有一个文档类型,允许用户使用多节点树选择器为页面选择警报。警报是另一种文档类型的实例。警报没有自己的页面,所以我想像部分一样使用他们选择的模板。我想做的是:

var alertIds = Model.Content.GetPropertyValue("alert");

List<umbraco.NodeFactory.Node> alerts = new List<umbraco.NodeFactory.Node>();

foreach (var alertId in alertIds.ToString().Split(','))
{
    alerts.Add(new umbraco.NodeFactory.Node(int.Parse(alertId)));
}

那么我可以做一个例子:

library.RenderTemplate(alerts[0].Id)

我想这样做是因为我喜欢这样的想法,即可以在 Umbraco 中选择模板并且只知道如何呈现自己,而不是在我的 MVC 项目中创建一个部分并在那边。然而,我运行进入以下错误:

"Error rendering template with id 1128: 'System.InvalidOperationException: A single instance of controller 'Umbraco.Web.Mvc.RenderMvcController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.\r\n at System.Web.Mvc.ControllerBase.VerifyExecuteCalledOnce()\r\n at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)\r\n at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)\r\n at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)\r\n at Umbraco.Web.Mvc.UmbracoMvcHandler.ExecuteUmbracoRequest()\r\n at Umbraco.Web.Templates.TemplateRenderer.RenderUmbracoRequestToString(RequestContext requestContext)\r\n at Umbraco.Web.Templates.TemplateRenderer.ExecuteTemplateRendering(TextWriter sw, PublishedContentRequest contentRequest)\r\n at Umbraco.Web.Templates.TemplateRenderer.Render(StringWriter writer)\r\n at umbraco.library.RenderTemplate(Int32 PageId, Int32 TemplateId)' "

提前感谢您查看此内容!

我明白你在尝试什么,但那不是方法。

Alerts don't have their own page so I wanted to use their selected template like a partial.

如果文档类型(节点)不是页面而只是数据容器,您应该不使用模板。这是因为 umbraco 会为那个节点生成一个 url,你会弄乱你的 SEO。

I wanted to do it this way because I like the idea that the templates can be chosen in Umbraco

您可以在您的文档类型(将列出所有可能模板的新数据类型)上创建一个下拉菜单 属性 来模仿模板选择器。当显示节点时,你会写这样的东西:

switch(alert.GetPropertyValue<string>("template"))
{
    case "News": RenderForNews(alert);
    case "Frontpage": RenderForFrontpage(alert);
    ...
}