2016-08-18 97 views
1

我在公開API端點的Orchard CMS(1.10)中創建了一個自定義模塊。我想公開一個Get調用,如果我傳遞了內容項的ID,它將返回該內容項的Html。Orchard CMS - 在API調用中返回HTML

我也想知道我怎樣在API調用中返回頁面佈局html?

謝謝

+0

我想知道* API模塊*是什麼?請提供更多細節。此外,爲什麼你將問題標記爲Orchard 1.6,1.8,然後你寫了1.10?那麼你實際使用哪個版本? – ViRuSTriNiTy

+0

通過contentManager獲取項目,構建IShapeDisplay.Display(myShape)的形狀和使用以及實例。將其作爲編碼字符串或任何您需要的返回。 – Xceno

回答

2

我敢肯定你所要求的是正是ItemControllerOrchard.Core.Contents.ControllersDisplay方法做:

public ActionResult Display(int? id, int? version) { 
    if (id == null) 
     return HttpNotFound(); 

    if (version.HasValue) 
     return Preview(id, version); 

    var contentItem = _contentManager.Get(id.Value, VersionOptions.Published); 

    if (contentItem == null) 
     return HttpNotFound(); 

    if (!Services.Authorizer.Authorize(Permissions.ViewContent, contentItem, T("Cannot view content"))) { 
     return new HttpUnauthorizedResult(); 
    } 

    var model = _contentManager.BuildDisplay(contentItem); 
    if (_hca.Current().Request.IsAjaxRequest()) { 
     return new ShapePartialResult(this,model); 
    } 

    return View(model); 
} 

視圖的代碼是這樣的:

@using Orchard.ContentManagement 
@using Orchard.Utility.Extensions 
@{ 
    ContentItem contentItem = Model.ContentItem; 
    Html.AddPageClassNames("detail-" + contentItem.ContentType.HtmlClassify()); 
}@Display(Model) 
+0

哇,我很難找到這個答案!正是我需要的。 – Nathan

3

我覺得你需要什麼:

public class HTMLAPIController : Controller { 
    private readonly IContentManager _contentManager; 
    private readonly IShapeDisplay _shapeDisplay; 
    private readonly IWorkContextAccessor _workContextAccessor; 

    public HTMLAPIController(
     IContentManager contentManager, 
     IShapeDisplay shapeDisplay, 
     IWorkContextAccessor workContextAccessor) { 
     _contentManager = contentManager; 
     _shapeDisplay = shapeDisplay; 
     _workContextAccessor = workContextAccessor; 
    } 

    public ActionResult Get(int id) { 
     var contentItem = _contentManager.Get(id); 

     if (contentItem == null) { 
      return null; 
     } 

     var model = _contentManager.BuildDisplay(contentItem); 

     return Json(
      new { htmlString = _shapeDisplay.Display(model) }, 
      JsonRequestBehavior.AllowGet); 
    } 

    public ActionResult GetLayout() { 
     var layout = _workContextAccessor.GetContext().Layout; 

     if (layout == null) { 
      return null; 
     } 

     // Here you can add widgets to layout shape 

     return Json(
      new { htmlString = _shapeDisplay.Display(layout) }, 
      JsonRequestBehavior.AllowGet); 
    } 
}