I'm trying to get the current page via a macro that is placed in an RTE. When the macro is added into the RTE, it looks for the current page, but I'm assuming this isn't possible as the changes have not been set, therefore can't get the current page?
I get the following error:
Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent
Received an error from the server
An error occured
Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.
Exception Details
System.InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.
And it fails in the following GetCurrentPage method:
public class UmbracoHelperWrapper : IUmbracoHelper
{
private readonly UmbracoHelper _umbracoHelper;
public UmbracoHelperWrapper(UmbracoHelper helper)
{
_umbracoHelper = helper ?? throw new ArgumentNullException(nameof(helper));
}
public IPublishedContent GetCurrentPage()
{
return _umbracoHelper.AssignedContentItem;
}
}
This happens whenever I call the macro into a page
In Razor, it works fine, however when trying to use the same line of code in a service class, it throws that error.
I've had a workaround, where I pass the model within the Razor file to the method (originally, I didn't want to pass but read the current page wherever the method is called)
The code was the following in the service class:
public Service(IUmbracoHelper umbracoHelper)
{
_umbracoHelper = umbracoHelper ?? throw new ArgumentNullException(nameof(umbracoHelper));
}
public Item GetPageDetails()
{
var home = _umbracoHelper.GetCurrentPage().Parent
}
Get Current page via a Macro?
I'm trying to get the current page via a macro that is placed in an RTE. When the macro is added into the RTE, it looks for the current page, but I'm assuming this isn't possible as the changes have not been set, therefore can't get the current page?
I get the following error:
Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent
Any suggestions?
EDIT:
I'm using the following to get the current page:
Umbraco.Web.Composing.Current.UmbracoHelper.AssignedContentItem
However, in the razor file, using Model.Content does get me the current page, but the above gets me a null
Hi Javz
Can you show me all code you use?
I use tested this macro partial view code and it works perfectly for me:
Thanks,
Alex
I've tried to replicate the issue again:
Received an error from the server An error occured Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent. Exception Details System.InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.
And it fails in the following GetCurrentPage method:
This happens whenever I call the macro into a page
Hi Alex,
Thanks for getting back to me!
Apologies for the late reply.
In Razor, it works fine, however when trying to use the same line of code in a service class, it throws that error.
I've had a workaround, where I pass the model within the Razor file to the method (originally, I didn't want to pass but read the current page wherever the method is called)
The code was the following in the service class:
Hi Javz
I believe if you inject UmbracoHelper - you will not get null there.
Thanks,
Alex
Hi Alex,
Correct me if I'm wrong, but I do believe it has been injected?
In the UmbracoHelperWrapper class, UmbracoHelper is injected as the following:
And this then is then injected into the service class as:
DI was not set up properly for the UmbracoHelper. In V7 I could do the following:
But with V8, it seems like I would need to populate all the values?
Any pointers in the correct direction?
Thanks
is working on a reply...