首页 > 解决方案 > “UmbracoHelper 是用 UmbracoContext 构造的,当前请求不是前端请求。”

问题描述

我正在尝试使用 Umbraco 实现 ajax 分页。

在服务器端,我有以下内容:

[System.Web.Http.HttpGet]
public JsonResult pagination(int? page)
{
IEnumerable<IPublishedContent> newsPosts = Umbraco.AssignedContentItem.DescendantOrSelf("news").Children.Where(x => x.IsVisible() && x.DocumentTypeAlias ==     "newsPost").OrderByDescending(x => x.UpdateDate).Take(5);

    //from here on we will be returning the json within which information required for displaying post entries in carousel is included.
    string json = "[some random string]"; //just random string for now.
    return Json(json, JsonRequestBehavior.AllowGet);
}

如您所见,我正在尝试从 IPublishedContents 中获取必要的数据,但在实例化这一系列 IPublishedContents 时遇到了麻烦。

这是我访问时遇到的错误:

Chrome 上的locahost:{port}/umbraco/surface/{controller}/pagination

Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request.

Details: System.InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request.

正如我所说,我从 Chrome 发出这个请求,我认为这意味着这个请求来自前端,所以我不确定我为什么会收到这个错误。

在搜索过程中我发现了这些

1)our.umbraco.com 论坛 2)stackoverflow 帖子

  1. 空无一人,没有答案,至于2,我觉得答案与我的情况不太相关。我想首先实例化 IPublishedContent 。

我的是 Umbraco 7。

有没有可能告诉我为什么前端的请求是不可取的?

任何提示将不胜感激。

谢谢,

标签: ajaxpaginationumbraco

解决方案


尝试以这种方式获取您的节点。

var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var yourNode = umbracoHelper.TypedContentAtXPath("umbracoPathtoYourNode");

推荐阅读