3

I am porting some code from Web Forms to MVC. In order to handle a case where query string parameters on the URL can change the landing page, I have added a RequestBeginProcessor to the mvc.requestBegin pipeline.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <pipelines>
        <mvc.requestBegin>
            <processor type="Company.Web.Pipelines.MvcRequestBeginProcessorPathResolver, Company.Web" 
                       patch:before="processor[@type='Sitecore.Mvc.Pipelines.Request.RequestBegin.SetupPageContext, Sitecore.Mvc']"
                       reusable="false" />
        </mvc.requestBegin>
    </pipelines>
</sitecore>

The processor examines the query string, stores values in Session (InProc), and if necessary does a redirect:

public class MvcRequestBeginProcessorPathResolver : RequestBeginProcessor
{
    public override void Process(RequestBeginArgs args)
    {
        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            ProcessQueryString();
            HttpContext.Current.Session["TEST_XYZZY"] = "xyzzy";

            //redirect if necessary
            if (_doRedirect)
            {
                // Works in webforms, endResponse = false required from Processor
                //Response.Redirect("/testpage/?" + _queryString);
                Response.Redirect("/testpage/?" + _queryString, false);
            }
        }
    }
}

After the redirect, the target page queries the values set in Session prior to the redirect.

In the legacy webforms version, this code is in the code behind of the layout (ascx.cs) and works with the single parameter version of Response.Redirect(String url).

When I attempt the same code from the Processor, the Session values are not available in the target page. The Session.SessionID is the same, but the values placed in session before the redirect are not there.

Changing the redirect call to Redirect(String url, bool endResponse) and passing false to endResponse, appears to give the desired outcome - the session values are available after the redirect.

Why would this Session behavior be different between a Web Forms layout code behind and an mvc.RequestBeginProcessor?

1

This has nothing to do with Sitecore's pipeline nor MVC vs WebForms but instead the issue stems from Redirects and Session variables age-long issue. Essentially, the thread aborts if Redirect is called with endResponse is true (default). Behind the scenes, it throws a ThreadAbortException which is actually pretty bad. It's always recommended to set endResponse to false and call the CompleteRequest method. Essentially, the session token gets lost when the server tells the browser (thru the header) to get another page. Here's a detailed blog post and actually poses another possible cause which has something to do with your hosting setup (i.e. webfarm, etc.) if that applies to you.

Btw, if you do end up setting endResponse to false and still have issues with the session variable, you may want to check if any of the processors after yours initializes the session. I didn't check this as it may not be relevant for this specific question yet.

1
  • Thanks @Marco. Setting the endResponse to false does work and the blog post was informative. I'm still not clear as to exactly why the code works with the default true in Web Forms but not in the mvc.RequestBeginProcessor. – fgantt Jan 24 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.