Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Heather Floyd 610 posts 1033 karma points MVP 6x c-trib
    Jul 13, 2023 @ 14:24
    Heather Floyd
    0

    v8 - HttpContext.Current.Session IS NULL in EditorModelEventManager.SendingContentModel event

    Hi friends,

    I am back-porting some code I wrote for a v10 site to use in a v8 site, but having issues...

    Basically, I am trying to get the currently-loading node Id (when opening a content node in the back-office for editing), and store that id so I can retrieve it for use in a property editor's rendering.

    Using the 'SendingContentModel' event, in my v10 site I was able to set a Session variable, but in v8, when I try to set a Session variable, the Session is always NULL:

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class StoreUmbracoSessionInfoEventComponent : IComponent
    {
        public void Initialize()
        {
            EditorModelEventManager.SendingContentModel += StoreUmbracoSessionInfo;
        }
    
        public void Terminate()
        {
            // Unsubscribe during shutdown
            EditorModelEventManager.SendingContentModel -= StoreUmbracoSessionInfo;
        }
    
            private void StoreUmbracoSessionInfo(
                System.Web.Http.Filters.HttpActionExecutedContext sender,
                EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
            {
                //if this is NOT an Element, Store the current node info in Session var
                if (e.Model.IsElement == false)
                {
                    HttpContext context = HttpContext.Current; //THIS IS NOT NULL
    
                    context.Session["UmbracoNode"]= e.Model.Id; //NULL REF ERROR HERE - .Session is NULL
                }
            }   
        }
    

    I have added the following to web.config to enable Session State - perhaps something is missing here?

    <system.web>
        ...
        <sessionState mode="InProc" timeout="20"  regenerateExpiredSessionId="true"/>
        ...
        <pages enableSessionState="true">
            ...
        </pages>
        ...
    </system.web>
    

    I also looked at storing it in a cookie, but couldn't get that to work either.

  • Heather Floyd 610 posts 1033 karma points MVP 6x c-trib
    Jul 13, 2023 @ 16:54
    Heather Floyd
    100

    Seems something was missing from the web.config - a 'Session' module:

    <system.webServer>
        ...
        <modules runAllManagedModulesForAllRequests="true" >
            ...
            <remove name="Session" />
            <add name="Session" type="System.Web.SessionState.SessionStateModule" />
        </modules>
        ...
    </system.webServer>
    

    After adding that, it now works as expected. 🙂

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies