Orchard CMS - 在 API 调用中返回 HTML

sho*_*wal 1 orchardcms orchardcms-1.6 orchardcms-1.8 orchardcms-1.9

我在 Orchard CMS (1.10) 中创建了一个自定义模块,它公开了一个 API 端点。我想公开一个 Get 调用,如果我传递内容项的 ID,它将返回该内容项的 Html。

我也想知道如何在 API 调用中返回页面布局 html?

谢谢

mda*_*eer 5

我认为这是你需要的:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)