Copied to clipboard

Flag this post as spam?

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


  • Jason 2 posts 72 karma points
    Apr 13, 2016 @ 23:13
    Jason
    0

    No session in PublishedContentRequest.Prepared?

    I'm quite new to Umbraco so please excuse my ignorance.

    I'm trying to hook into the request pipeline at PublishedContentRequest.Prepared so that I can get the content id of the page that the user is requesting, and then store that ID in the user's session so that I can use it later, however the session does not seem to exist at this point.

    If I hook into the PreRequestHandlerExecute event I can get the session (and determine whether it is new or not), however this seems to execute after the PublishedContentRequest.Prepared, and also I'm not sure how to get the content id at that point.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
       PublishedContentRequest.Prepared += PublishedContentRequest_Prepared;
    }
    
    private void PublishedContentRequest_Prepared(object sender, EventArgs e)
    {
        PublishedContentRequest request = sender as PublishedContentRequest;
        if (request == null || !request.HasPublishedContent) return;
        var contentId = request.PublishedContent.Id;
    
        // No session seems to exist at this point?
        // request.RoutingContext.UmbracoContext.HttpContext.Session is null
        // HttpContext.Current.Session is null
    }
    

    Is there a way to use the session from within the PublishedContentRequest.Prepared event, or get the content id from within the PreRequestHandlerExecute event? My end goal is to store the content id of the current requested page in the user's session, if there's another way to go about that please let me know!

  • Samira 113 posts 452 karma points
    Apr 14, 2016 @ 12:44
  • Jason 2 posts 72 karma points
    Apr 14, 2016 @ 23:29
    Jason
    0

    Thanks Samira, but no, at line 20 when the context is retrieved, the session appears to be null

  • Will Roscoe 5 posts 90 karma points
    Nov 16, 2017 @ 15:24
    Will Roscoe
    0

    Hi Jason, Did you find a solution to this? I'm trying to access the session here too. Many thanks, Will

Please Sign in or register to post replies

Write your reply to:

Draft