I'm trying to create a UmbracoApiController to retrieve content from the CMS via ajax using Umbraco 7.1.4
I can make requests to the controller perfectly fine, however the UmbracoHelper throws the following exception when trying to retrieve the AssignedContentItem property:
"Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request."
My code is below:
public class TopicsController : UmbracoApiController
{
private IContentService _service;
public TopicsController(IContentService service)
{
_service = service;
}
public IEnumerable<Topic> GetTopics()
{
return GetTopics(0);
}
public IEnumerable<Topic> GetTopics(int topic)
{
return _service.GetTopics(Umbraco.AssignedContentItem, topic);
}
}
Please can someone tell me why this isn't working? I would like to retrieve content from the CMS via ajax and was able to do this in v6
I think because you are performing an ajax api request and you are not requesting a page, UmbracoContext is throwing a wobbler. Some of the properties within UmbracoContext rely on the request being made to be an actual page within Umbraco. So things like CurrentPage will always be null in your instance as you are making an api request.
I'd suggest passing the ID of the current page from your client side ajax request to the api controller to see if that works. That way, you'll be able to use the Content Service to retrieve the page and then subsequent topics.
UmbracoApiController - Cannot use UmbracoHelper
I'm trying to create a UmbracoApiController to retrieve content from the CMS via ajax using Umbraco 7.1.4
I can make requests to the controller perfectly fine, however the UmbracoHelper throws the following exception when trying to retrieve the AssignedContentItem property:
"Cannot return the IPublishedContent because the UmbracoHelper was constructed with an UmbracoContext and the current request is not a front-end request."
My code is below:
Please can someone tell me why this isn't working? I would like to retrieve content from the CMS via ajax and was able to do this in v6
Hi Richard,
I think because you are performing an ajax api request and you are not requesting a page, UmbracoContext is throwing a wobbler. Some of the properties within UmbracoContext rely on the request being made to be an actual page within Umbraco. So things like CurrentPage will always be null in your instance as you are making an api request.
I'd suggest passing the ID of the current page from your client side ajax request to the api controller to see if that works. That way, you'll be able to use the Content Service to retrieve the page and then subsequent topics.
Thanks, Dan.
Thanks Dan :)
is working on a reply...