ajax - “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 帖子
- 空无一人,没有答案,至于2,我觉得答案与我的情况不太相关。我想首先实例化 IPublishedContent 。
我的是 Umbraco 7。
有没有可能告诉我为什么前端的请求是不可取的?
任何提示将不胜感激。
谢谢,
解决方案
尝试以这种方式获取您的节点。
var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var yourNode = umbracoHelper.TypedContentAtXPath("umbracoPathtoYourNode");
推荐阅读
- sql-server - 查找重复 SQL Server 的最有效方法
- python - 如何使用 python 和美丽的汤刮这张桌子?
- ruby-on-rails - RoR 验证依赖模型但如果依赖验证失败则不停止父模型
- apache-spark - 在 Spark 中使用复杂的条件和滞后的自引用创建新列
- javascript - 使用 reactjs 从我的 fetch 中获取元素到 render()
- php - phpexcel创建错误的文件
- html - 身体元素上出现奇怪的边距
- google-chrome - 浏览器 localStorage 是如何只读的?
- google-apps-script - 从选项卡名称列表中为每个工作表生成 URL
- java - 查找两个字符串之间的匹配字符