updating session variable on every page load

Rita Mikusch asked on January 23, 2017 19:40

Hi,

NOTE: version 8.0

There's a session variable I'd like to update on every single non-admin webpage load. (It's okay if the session variable is updated on an admin webpage load ... but it doesn't need to be.)

Which handler would you recommend for that? Many of these handlers only run under specific conditions ... I'm not sure which handler would run on every webpage load?

I tried to add the session update code to CMSModuleHandler's init() method ... but it causes an error, I'm guessing the session isn't set up yet?

Thanks, Rita.

Correct Answer

Peter Mogilnitski answered on January 23, 2017 20:30

What you looking is Handling Global Events and Global System Events - Request Events

[CustomRequestEventsHandler]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers
    /// </summary>
    private class CustomRequestEventsHandlerAttribute : CMSLoaderAttribute
    {

        /// <summary>
        /// Called automatically when the application starts
        /// </summary>
        public override void Init()
        {

            /// <summary>
            /// Occurs when the request processing starts.
            ///
            RequestEvents.Prepare.Begin  += (sender, args) =>
            {
                // Manipulate your session variables
            }

        }
    }
} 
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 23, 2017 19:52

Not sure you'd use an event handler. Create a public static property. Or even better yet, create a webpart and place it on your master page which will get or set your session variable. Doing this will allow you to set some other properties like if you want to turn it on or off at all from the UI.

1 votesVote for this answer Mark as a Correct answer

Rita Mikusch answered on January 23, 2017 20:02

Thank you, Brenden, I didn't think of using a webpart.

I've been working on a couple of security handlers, and kinda got tunnel vision :).

0 votesVote for this answer Mark as a Correct answer

Rita Mikusch answered on January 23, 2017 20:45

Excellent, thank you, Peter!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.