0

All, I'm trying to implement an HttpModule (IHttpModule) to catch pages request and redirect to a new page. Unfortunately, it seems I can't use the Session in the new page. because the Session is null.

Here is my code looks like. please review it .

public class MyModule : IHttpModule
{
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);

        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            ....
            HttpContext.Current.Server.Transfer("newpage.aspx");//redirect to new page.
        }
}

In the newpage.aspx, There is an exception says Object reference not set to an instance of an object for the code HttpContext.Current.Session[xxx], Because the HttpContext.Current.Session is null . Could someone tell me what happen to it ? Thanks.

Update

All, I found If I use the HttpContext.Current.Response.Redirect to redirect url . Everything is ok. I mean the Session object is initiated before being used.But that doesn't work for Server.Transfer.

I already knew what is the difference of these two.

2
  • Can you show the URL of the first and the second page? You can probably going to a seperate domain of to a diffrent protocol.
    – Peter
    Jul 10, 2013 at 9:48
  • @peer Both of them are in the same app. the urls look like from http://localhost:7778/orginalpage.aspx redirect to http://localhost:7778/b/c/newpage.aspx. thanks.
    – Joe.wang
    Jul 10, 2013 at 9:57

1 Answer 1

0

The normal aspx runtime pipeline with 2 modules is:

--> HttpModule_1.BeginRequest();  --> HttpModule_2.BeginRequest(); --> HttpHandler(Page)
<-- HttpModule_1.EndRequest();  <-- HttpModule_2.EndRequest(); <-- HttpHandler(Page)

Imagine HttpModule_1 as your custom module and HttpModule_2 as the aspx session module.

Your custom module is running first, so no session is filled in when your module is running.

When HttpModule_1 is running BeginRequest you add a Server.Trasfer(). Now, the server transfer will execute the HttpHandler of the request page immediately, it will be called without leaving the BeginRequest and after the handler is done the transfer method will run the Request.End() and terminate the processing.

So, the handler for the "newpage.aspx" will also run before the session module. (actually, the session module will never run because Server.Transfer() will end the request).

If you guaranty that the session module is processes before your module you should resolve the problem (see https://stackoverflow.com/a/2427632/953144).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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