“ UmbracoHelper是使用UmbracoContext构造的,当前请求不是前端请求。”

时间:2018-11-16 04:56:16

标签: ajax pagination umbraco

我正在尝试使用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:在Chrome上{port} / umbraco / surface / {controller} /分页

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。

能否告诉我为什么不希望前端发出请求?

任何提示将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:0)

也许更容易使用网络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

通过标准的jajax调用,这将返回json,而无需序列化。

答案 1 :(得分:0)

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

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