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) { }
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.
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.
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.
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);
}
}
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.
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.
I have the same issue in Umbraco 10 with the following simple line in a surface controller:
where
_umbracoHelper
was constructed with the DI.However
SurfaceController
has a propertyCurrentPage
. I have just assigned it to my model in the controller.I got this error message when trying to access the
CurrentPage
from a surface controller. I neededCurrentPage
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 theUmbracoHelper
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:
I tried a lot of things to attempt get this to work and to make my surface controller testable:
UmbracoHelper
into the controllerIUmbracoHelperAccessor
and using that to create theUmbracoHelper
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 directCurrentPage
approach.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.
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
is working on a reply...