簡體   English   中英

Orchard CMS-在API調用中返回HTML

[英]Orchard CMS - Returning HTML in API call

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

我也想知道如何在API調用中返回Page布局html?

謝謝

我認為這是您需要的:

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);
    }
}

我很確定您要問的是Orchard.Core.Contents.Controllers ItemControllerDisplay方法到底在做什么:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM