Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Richard Read 16 posts 47 karma points c-trib
    Aug 14, 2014 @ 10:28
    Richard Read
    0

    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:

        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

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 15, 2014 @ 09:45
    Dan Lister
    100

    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.

  • Richard Read 16 posts 47 karma points c-trib
    Aug 15, 2014 @ 14:10
    Richard Read
    0

    Thanks Dan :)

Please Sign in or register to post replies

Write your reply to:

Draft