I upgraded from Epi 12.8.0 to Epi 12.20.0. After the upgrade, I noticed that the publishing event is unable to capture the URL of the page that has been published. It worked fine in version 12.8.0, but not in the current version.
Is there a proper way to retrieve the page URL when publishing content using the IContent type object?
This is the code I'm using to get URL
This is initialization
public void Initialize(InitializationEngine context)
{
_httpContextAccessor = context.Locate.Advanced.GetService<IHttpContextAccessor>();
IConfiguration _configuration = context.Locate.Advanced.GetService<IConfiguration>();
_environment = context.Locate.Advanced.GetService<IWebHostEnvironment>();
if (_contentEvents == null)
{
_contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
}
_contentEvents.PublishedContent += PublishedContent;
}
private void PublishedContent(object sender, ContentEventArgs e)
{
IContent content = e.Content;
var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var pageUrl = urlResolver.GetUrl(content.ContentLink);
}
In here this returns page url null
You should consider using constructor injection over the ServiceLocator
class.
Are you positive the content has a template? In other words, are you able to browse to it? Otherwise GetUrl()
will return null
.
You're currently just looking for IContent
(which may or may not be routable) so it might be better to do something like this for pages:
if (e.Content is PageData publishedPage)
{
var url = _urlResolver.GetUrl(publishedPage);
if (url != null)
{
// Do something with the URL
}
else {
// Page has no URL, for example because there's no template for it
}
}