More in this section

Forums / Developing with Sitefinity / Run something every time.

Run something every time.

2 posts, 0 answered
  1. jcrichto
    jcrichto avatar
    35 posts
    Registered:
    14 Aug 2012
    21 Feb 2009
    Link to this post
    I have a need to run code on every page.  In a normal situation, I could put code in the masterpage codebehind.  However, I am noticing that caching prevents the codebehind from running on each request.  How can I get around this and force my code to run on EVERY load?  I tried building a IService, but I couldn't get it to run at all.


  2. Vlad
    Vlad avatar
    498 posts
    Registered:
    19 Jun 2017
    21 Feb 2009
    Link to this post

    Hello Jeremy,

    When the page is cached, no page handler for it is created by the ASP.NET application.
    In case your custom code does not require an instance of the Page object, I am suggesting you to put the code in an HttpModule and load it after our CmsHttpModule. To accomplish this, you can do the following:

    1. Create a class CustomHttpModule in the App_Code (CustomHttpModule.cs):

    using System;  
    using System.Web;  
    using Telerik.Cms.Web;  
     
    public class CustomHttpModule : IHttpModule  
    {  
        public void Init(HttpApplication context)  
        {  
            context.BeginRequest += new EventHandler(context_BeginRequest);  
        }  
     
        void context_BeginRequest(object sender, EventArgs e)  
        {  
            // If CmsUrlContext.Current is not null, a CMS page is requested,   
            // the returned ICmsUrlContext object contains the necessary information about the page.  
            ICmsUrlContext urlContext = CmsUrlContext.Current;  
            if (urlContext != null)  
            {  
                // Add your code here  
     
                // Check if the requested page will be loaded in public mode  
                if (HttpContext.Current.Items[UrlHelper.EditModeKey] == null)  
                {   
                      
                }  
            }  
        }  
     
        public void Dispose()  
        {  
        }  
    }  
     

    2. Declare the module in the web.config bellow the CmsHttpModule:

          <add name="Cms" type="Telerik.Cms.Web.CmsHttpModule, Telerik.Cms"/>  
          <add name="CustomModule" type="CustomHttpModule"/>  
     

    I hope this is helpful. 



    Sincerely yours,

    Vlad
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
2 posts, 0 answered