【问题标题】:"the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request."“UmbracoHelper 是用 UmbracoContext 构造的,当前请求不是前端请求。”
【发布时间】:2018-11-23 17:18:26
【问题描述】:

我正在尝试使用 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 时遇到问题。

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

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

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 forum 2)stackoverflow post

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

我的是 Umbraco 7。

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

任何提示将不胜感激。

谢谢,

【问题讨论】:

  • 如果您发现我的回答是“我需要做的就是创建一个...的新实例”,请忘记它,我以为我做到了,但意识到这是因为我刚刚注释掉了关键线
  • 您的控制器是否继承自 Surface?

标签: ajax pagination umbraco


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    也许更容易使用 web api

    创建一个继承自 UmbracoApiController 的控制器

    public class PagedItemsController : UmbracoApiController
    {
        [HttpGet]
        [ActionName("list")] //Optional see note below
        public IHttpActionResult GetItems([FromUri] int pageNo = 1)
        {
            // Next you need some way of getting the items you need. 
            // I would not return the whole IPublishedContent items.  Rather query those and then use linq Select to transform into a more relevant smaller class (not doing this here)
            // I've just included this for brevity
            var items = _itemService.GetPagedItems(pageNo);
    
            // Now return the results
            return Ok(items);
        }
    }
    

    在 Umbraco 中对端点的调用遵循格式

    /umbraco/api/{controller}/{endpoint}
    

    使用上面的[ActionName("list")] 调用GetItems 方法将是

    http://example.com/umbraco/api/PagedItems/list?pageNo=3
    

    如果没有 ActionName 属性,调用将是

    http://exampe.com/umbraco/api/PagedItems/GetItems?pageNo=3
    

    使用标准的 jquery ajax 调用,这将返回 json 而无需序列化。

    【讨论】:

    • 感谢您的回答,但我仍然无法在控制器中实例化 ipublishedcontents。同样,“Umbraco Helper 的构造带有...”错误。 // 接下来你需要一些方法来获取你需要的项目。 // 我不会返回整个 IPublishedContent 项。而是查询那些,然后使用 linq Select 转换成一个更相关的小类(这里不这样做)=>如果你能给我更多的细节会很棒(参数和属性名称可以只是一个模拟,但会喜欢实际的方法。谢谢,
    • 那么您是否尝试通过将 url 放在 Chrome 地址栏中来调用此端点?如果您不包含对 Umbraco 方法的调用,您的端点是否会返回任何内容?
    • 感谢所有信息,(1)我不打算使用浏览器查看控制器的结果,所以我明白你的意思。 (2) 我在实例化 Umbraco Helper 时遇到问题,结果我应该使用“TypedContent”而不是“AssignedContentItem”(我没有提供足够的信息,所以这是我的错)
    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2021-11-16
    • 2012-08-12
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多