Copied to clipboard

Flag this post as spam?

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


  • Ryan Carlin 1 post 71 karma points
    Dec 04, 2019 @ 16:29
    Ryan Carlin
    0

    Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent

    I have the below surface controller that has an Ajax call from the UI but when it tries to instantiate the SelectViewModel() it is giving me an error with the UmbracoHelper.

    "System.InvalidOperationException: 'Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.'"

    Am I supposed to instantiate the UmbracoHelper again or can I not create ViewModelObjects within an Ajax call? Any help appreciated.

    public class SelectSurfaceController : BaseSurfaceController
    {
        [HttpGet]
        public ActionResult TravellersTemplate(int count)
        {
            var model = new SelectViewModel();
            model.TravellersDatesOfBirth = Enumerable.Range(1, count).Select(x => new DateTime()).ToList();
    
            return PartialView("_Travellers", model);
        }
    
    
    
    public class SelectViewModel : LayoutViewModel
    {
        public SelectViewModel() : base(Current.UmbracoHelper.AssignedContentItem) { }        
        public SelectViewModel(IPublishedContent content) : base(content) { }
    
  • Mike Ryan 5 posts 75 karma points
    Oct 15, 2021 @ 02:11
    Mike Ryan
    0

    Did you ever figure this one out Ryan? I was trying to do nearly the same thing, and I'm sure quite a few other people too. They discuss it here and link to some documentation. I personally just created an ApiClient that I could call from a template view and returned a custom ViewModel I created from Shopify product info, completely outside of the Umbraco IPublishedContentModels. A bit of overengineering in my opinion, but I don't pretend to be an expert.

  • ewuski 99 posts 286 karma points
    Jul 27, 2022 @ 12:19
    ewuski
    1

    I have the same issue in Umbraco 10 with the following simple line in a surface controller:

    var aaa = _umbracoHelper.AssignedContentItem;
    

    where _umbracoHelper was constructed with the DI.

    However SurfaceController has a property CurrentPage. I have just assigned it to my model in the controller.

  • Emma Garland 41 posts 123 karma points MVP 7x c-trib
    Nov 30, 2022 @ 11:18
    Emma Garland
    1

    I got this error message when trying to access the CurrentPage from a surface controller. I needed CurrentPage to access the search settings on the root node. While this worked fine in the code itself, I needed the controller method to be testable so I tried to switch to using the UmbracoHelper approach. That is when I started seeing this error.

    I found the error message in the code line https://github.com/umbraco/Umbraco-CMS/blob/b69afe81f3f6fcd37480b3b0295a62af44ede245/src/Umbraco.Web.Common/UmbracoHelper.cs#L70, which says:

    In some cases accessing this property will throw an exception if there is not IPublishedContent assigned to the Helper

    this will only ever happen if the Helper is constructed via DI during a non front-end request

    I tried a lot of things to attempt get this to work and to make my surface controller testable:

    • Firstly by injecting the UmbracoHelper into the controller
    • Next by injecting IUmbracoHelperAccessor and using that to create the UmbracoHelper

    Everything I tried gave me the same error message "'Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.". This happened both in the code execution, and the unit tests themselves, despite setting up the expectations with various factories.

    In the end, I abstracted the dependency away by injecting my own IContentRetriever service and accessing the search settings I needed in the SurfaceController, rather than the direct CurrentPage approach.

  • Salomons 15 posts 107 karma points c-trib
    May 15, 2023 @ 14:19
    Salomons
    0

    In Umbraco 7 we had the EnsurePublishedContentRequestAttribute to help us out.

    One workaround i implemented in Umbraco 10 was the following line in the action method of the api controller.

    // create Umbraco PublishedRequest for the Macro's to work.                                   
    this.UmbracoContext.PublishedRequest = new PublishedRequest(this.UmbracoContext.OriginalRequestUrl, this.UmbracoContext.OriginalRequestUrl.AbsolutePath, categoryContent, false, null, null, CultureInfo.CurrentUICulture.Name, null, null, null, null, false, true);
    
  • Marcio Goularte 388 posts 1360 karma points
    May 16, 2023 @ 19:49
    Marcio Goularte
    0

    Unfortunately in ajax calls to Suface the CurrentPage object and the AssignedContentItem in UmbracoHelper are null.

    An alternative would be to inject an instance of IPublishedContentQuery and pass the node id as a parameter

    public class SelectSurfaceController : BaseSurfaceController
        {
            private readonly IPublishedContentQuery _publishedContentQuery;
            public SelectSurfaceController(IPublishedContentQuery publishedContentQuery) 
            {
                _publishedContentQuery = publishedContentQuery
            }
    
            [HttpGet]
            public ActionResult TravellersTemplate(int count, int nodeId)
            {
    
                var content = _publishedContentQuery.Content(nodeId);
                var model = new SelectViewModel(content);
                model.TravellersDatesOfBirth = Enumerable.Range(1, count).Select(x => new DateTime()).ToList();
    
                return PartialView("_Travellers", model);
            }
    
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft