我在 Orchard CMS (1.10) 中创建了一个自定义模块,它公开了一个 API 端点。我想公开一个 Get 调用,如果我传递内容项的 ID,它会返回该内容项的 Html。
我也想知道如何在 API 调用中返回页面布局 html?
谢谢
我在 Orchard CMS (1.10) 中创建了一个自定义模块,它公开了一个 API 端点。我想公开一个 Get 调用,如果我传递内容项的 ID,它会返回该内容项的 Html。
我也想知道如何在 API 调用中返回页面布局 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);
}
}
我很确定您要问的正是 in 的Display
方法ItemController
在Orchard.Core.Contents.Controllers
做什么:
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)