在 Umbraco 7.0.3 I 中:
- 使用宏容器的属性编辑器创建了一个名为宏容器的数据类型
- 已创建名为“联系表单”的文档类型,其中包含名为“主体”的属性和“宏容器”类型
- 创建了名为 _contactForm.cshtml 的部分 View (在 Views\MacroPartials 中)
- 使用 MVC 部分 View _contactFrom.cshtml 创建了名为“联系表单”的宏
- 添加了名为“联系我们”的联系表单类型的内容
- 将联系表单宏添加到“联系我们”页面中名为“正文”的宏容器属性
然后我有一个 Surface Controller
,我用一些 AJAX
调用它来显示页面(更具体地说是页面的 Body 属性):
public class JsController : SurfaceController
{
public ActionResult GetPage(int id)
{
var page = new Node(id);
if (page == null || page.GetProperty("body") == null)
return Content(@"Hmm, something went wrong. Unable to find what you're looking for.");
return Content(page.GetProperty("body").Value);
}
}
这个设置几乎可以工作,但问题是返回的不是渲染的表单,而是:
<!--?UMBRACO_MACRO macroAlias="ContactForm" /-->
所以现在我需要渲染这个宏\表单\部分 View ...我认为我可能需要在 Controller 中执行此操作,但如果我可以在另一侧(通过Javascript)执行此操作,则可以作为出色地。我可以在 Controller 中调用 Umbraco 函数来根据页面 id 和宏别名呈现宏吗?
最佳答案
因此,在花了几个小时对 Umbraco
团队的这一过程是多么愚蠢感到愤怒之后,阅读了类似 this 的帖子。和 this ,我终于想出了一个相当难看但有效的方法...如果 PublishedContentRequest 类构造函数不是内部的,事情会简单得多!
无论如何,这就是我必须做的:
1)扩展EnsurePublishedContentRequestAttribute
public class CreatePublishedContentRequestAttribute
: EnsurePublishedContentRequestAttribute
{
public CreatePublishedContentRequestAttribute() : base(0) { }
protected override void ConfigurePublishedContentRequest(
PublishedContentRequest publishedContentRequest,
ActionExecutedContext filterContext)
{
var contentId = filterContext.RouteData.Values["id"];
int id = 0;
if (contentId != null && int.TryParse(contentId.ToString(), out id))
{
var content = UmbracoContext.ContentCache.GetById(id);
publishedContentRequest.PublishedContent = content;
var defaultLanguage = Language.GetAllAsList().FirstOrDefault();
publishedContentRequest.Culture = (defaultLanguage == null)
? CultureInfo.CurrentUICulture
: new CultureInfo(defaultLanguage.CultureAlias);
publishedContentRequest.ConfigureRequest();
HttpContext.Current.Session["PublishedContentRequest"]
= publishedContentRequest;
}
}
}
2) 重定向到用此属性修饰的操作,该属性重定向回我的 GetPage 操作并从 Session
检索 PCR
。现在我们可以渲染我们的宏:
public ActionResult GetPage(int id)
{
var publishedContent = UmbracoContext.ContentCache.GetById(id);
if (publishedContent == null || publishedContent.GetProperty("body") == null)
{ return Content(@"Unable to find what you're looking for."); }
if (UmbracoContext.PublishedContentRequest == null
&& Session["PublishedContentRequest"] == null)
{ return RedirectToAction("CreatePublishedContentRequest", new { id }); }
UmbracoContext.PublishedContentRequest =
(PublishedContentRequest) Session["PublishedContentRequest"];
Session["PublishedContentRequest"] = null;
UmbracoContext.HttpContext.Items["pageID"] = id;
return Content(GetHtmlContent(publishedContent));
}
[CreatePublishedContentRequest]
public ActionResult CreatePublishedContentRequest(int id)
{
return RedirectToAction("GetPage", new { id });
}
private string GetHtmlContent(IPublishedContent publishedContent)
{
string content = publishedContent.GetProperty("body").Value.ToString();
if (string.IsNullOrEmpty(content) || !content.Contains("UMBRACO_MACRO"))
{ return content;}
int startIndex = content.IndexOf("macroAlias=") + 12;
int length = content.LastIndexOf('"') - startIndex;
var macroAlias = content.Substring(startIndex, length);
return (Umbraco.RenderMacro(macroAlias) ?? new HtmlString("")).ToString();
}
这可行,但这是一些非常hacky的东西。如果 Umbraco
团队将 PublishedContentRequest
构造函数设为 public
,这可能会变得更加简洁。当然,可能有更好的方法来做到这一点,如果是的话,我洗耳恭听。
关于c# - Umbraco 7 局部 View 宏观渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21899185/