November Happy Hour will be moved to Thursday December 5th.
AI OnAI Off
November Happy Hour will be moved to Thursday December 5th.
There's no edit button for the first post...
Here's the modified code:
[InitializableModule] [ModuleDependency(typeof(ServiceContainerInitialization))] public class DependencyResolution : IConfigurableModule { private IContentEvents _contentEvents; public void ConfigureContainer(ServiceConfigurationContext context) { ... } public void Initialize(InitializationEngine context) { if (_contentEvents == null) { _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>(); } _contentEvents.PublishedContent += contentEvents_PublishedContent; } void contentEvents_PublishedContent(object sender, ContentEventArgs e) { if (e.Content is PageData) { var externalUrl = GetExternalUrl(e.ContentLink); using (var client = new HttpClient()) { var html = client.GetStringAsync(externalUrl).Result; } } } public static string GetExternalUrl(ContentReference content) { var internalUrl = UrlResolver.Current.GetUrl(content); var url = new UrlBuilder(internalUrl); Global.UrlRewriteProvider.ConvertToExternal(url, null, Encoding.UTF8); string externalUrl = HttpContext.Current == null ? UriSupport.AbsoluteUrlBySettings(url.ToString()) : HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + url; return externalUrl; } public void Uninitialize(InitializationEngine context) { if (_contentEvents == null) { _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>(); } _contentEvents.PublishedContent -= contentEvents_PublishedContent; } }
client.GetStringAsync() inside contentEvents_PublishedContent method returns old HTML.
I've managed to get correct html like this:
private void contentEvents_PublishedContent(object sender, ContentEventArgs e) { if (e.Content is PageData) { new Task(() => { ParsePage(e.ContentLink); }).Start(); } }
Is there a better solution? Thanks!
I would guess that the reason you see "old" data is that your event handler is executed before the event handler that evicts the page from cache. Try to change your type in ModuleDependency attribute to EPiServer.Web.InitializationModule
Thank you!
I thought I could use one module for both StructureMap and content events.
Hi everyone,
I'm trying to attach to IContentEvents.PublishedContent event, get external URL of the page, and do something with HTML.
ContentEventArgs.Content contains updated values, but when I try to parse http://localhost:65316/about-us/news-events/events/ using System.Net.Http.HttpClient, I get the old values.
The code looks like this:
I tested this with Alloy, versions 8.0 and 8.8
Any help would be greatly appreciated!