Try our conversational search powered by Generative AI!

Intercept when SitePage is passed to a controller method

Vote:
 

I kind of have the same problem as this person: http://stackoverflow.com/questions/29389926/why-is-the-icontentevents-loadedcontent-event-fired-multiple-times-for-a-page

I basicaly want to intercept whenever SitePage is passed to a controller method.

I tried using IContentLoader.LoadedContent, but if the method is fired about 20 times for each page request then I can't use it.

Can anybody outhere enligten me on the subject?

Here is my code:

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class LoadedContentEventInitializationModule : IInitializableModule
    {
        //internal Injected CustomPageLoaderService;

        public void Initialize(InitializationEngine context)
        {
            //Add initialization logic, this method is called once after CMS has been initialized            
            var contentEvents = ServiceLocator.Current.GetInstance();
            contentEvents.LoadedContent += contentEvents_LoadedContent;

        }

        private void contentEvents_LoadedContent(object sender, ContentEventArgs e)
        {
            var pageData = e.Content as SitePage;
            if (pageData != null)
            {
                //CustomPageLoaderService.Service.LoadPage(pageData);
            }
        }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic            
            var contentEvents = ServiceLocator.Current.GetInstance();            
            contentEvents.PublishingContent -= contentEvents_LoadedContent;
        }
    }
#171690
Nov 15, 2016 10:02
Vote:
 

Depends on what you want to do but for example a custom action filter lets you run code before the action is called.

#171705
Nov 15, 2016 15:13
Vote:
 

Yeah, I guessed as much. I just thought that there was some better way of implementing it.

Thanks!

This is what I did:

[TemplateDescriptor(Inherited = true)]
    [LoadCustomData]
    public class DefaultPageController : PageController<PageData>
    {
        public virtual ActionResult Index(PageData currentPage)
        {
            return View($"~/Views/{currentPage.GetOriginalType().Name}/Index.cshtml", currentPage);
        }
    }
    public class LoadCustomDataAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            object currentPage;
            if (filterContext.ActionParameters.TryGetValue("currentPage", out currentPage))
            {
                ServiceLocator.Current.GetInstance<ICustomPageLoaderService>().LoadCustomData(currentPage as PageData, filterContext.HttpContext.User);
            }
            base.OnActionExecuting(filterContext);
        }
    }




#171716
Nov 16, 2016 8:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.