2014-02-20 66 views
2

在一把umbraco 7.0.3 I:一把umbraco 7管窥宏观渲染

  1. 创建了一个名为大容器用大容器的属性编辑器中数据类型
  2. 创建的文档类型称为联系表与物业叫机身采用类型宏集装箱
  3. 创建了一个名为_contactForm.cshtml(在视图\ MacroPartials)管窥
  4. 称为联系表与MVC局部视图_contactFrom.cshtml
  5. 添加的共创建的宏型触点形式的ntent称为联系我们
  6. 添加联系表格宏名为Body的宏容器属性在我的联系我们页面

然后我有一个Surface Controller,我与一些AJAX来电显示的页面(更特别是页面的主体属性):

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" /--> 

所以现在我需要渲染这个宏\ form \ partial view ...我认为我可能需要在控制器中完成它,但是如果我可以在另一侧(通过Javascript)好。是否有一个Umbraco函数,我可以在控制器中调用以基于页面ID和宏别名渲染宏?

回答

5

所以花费几个小时在Umbraco团队如何痛苦愚蠢做出这个过程中发烟,读线程像thisthis后,我终于想通了一个相当难看,但工作方式......事情会一直这样简单多了如果PublishedContentRequest类构造函数不是internal

不管怎么说,这是我必须做的: 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操作的操作和检索SessionPCR。现在我们可以渲染我们的宏:

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,这可能会更加清晰。当然,也许有更好的方法来做到这一点,如果是的话,我全部都是耳朵。

+0

'PublishedContentRequest'类构造函数现在在更新的Umbraco版本中公开,但是我无法找到适合此用例的用法。 –

-3

您的控制器名称需要在其名称中包含“表面”。

JsSurfaceController 

此外,将[HttpPost]属性添加到ActionResult方法。

http://our.umbraco.org/documentation/Reference/Mvc/surface-controllers http://our.umbraco.org/documentation/Reference/Mvc/forms

+2

这些建议都不需要,甚至是错误的。如果我有两个同名的方法,或者我试图阻止GET流量,那么我只是需要'[HttpPost]',而我实际上只有GET流量进入它。“Surface”交易只是一个建议......实际上并不需要。 –

0

是不是有可能使用umbraco.library.RenderMacroContent?

+0

没有经过测试的代码示例的评论不是很有用。 –