【问题标题】:Orchard CMS - Returning HTML in API callOrchard CMS - 在 API 调用中返回 HTML
【发布时间】:2016-08-18 23:26:45
【问题描述】:

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

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

谢谢

【问题讨论】:

  • 我想知道 API 模块 是什么?请提供更多细节。另外,为什么您将问题标记为 Orchard 1.6、1.8,但您却写了 1.10?那么您实际使用的是哪个版本?
  • 通过 contentManager 获取项目,构建形状并使用 IShapeDisplay.Display(myShape) 的实例。将其作为编码字符串或您需要的任何内容返回。

标签: orchardcms orchardcms-1.6 orchardcms-1.8 orchardcms-1.9


【解决方案1】:

我认为这是你需要的:

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

【讨论】:

    【解决方案2】:

    我很确定您要问的正是Orchard.Core.Contents.ControllersItemControllerDisplay 方法正在做什么:

    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)
    

    【讨论】:

    • 哇,我真的很难找到这个答案!正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 2017-09-05
    相关资源
    最近更新 更多