Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.Transfer causing Session exception

In my global I have the following code to handle when an error occurs

//[..] code goes here
  Server.Transfer("~/Error.aspx?ErrorID=" + errorId);

It used to be a Response.Redirect which worked perfectly except that it changed the url (which is why I want to use Server.Transfer)

Unfortunately, now when it tries to load the Error page, it crashes on the Masterpage when it tries to refer to the Session

HttpException:
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \\ section in the application configuration.

I do have enableSessionState in both my config and my page.

I also found some links which suggest using Context.RewritePath - that just causes a blank page to load for me.

Using Response.Redirect works perfectly and as expected, so I assume Server.Transfer is the issue here. What is it?

EDIT Code:

protected void Application_Error(object sender, EventArgs e)
        {

            lock (_lockMe)
            {
                Exception ex = Server.GetLastError();

                if (ex != null)
                {
                    if (ex.InnerException != null)
                        ex = ex.InnerException;

                    ErrorLoggingManager.AddError(ex, new MembershipData(), ...); //etc
                }

                Server.ClearError();

                   //Some other database code for cleaning up some stuff when an error happens

                }

                try
                {
                    if (Response != null)
                    {
                        //Get the last error logged
                        MyDataContext db = new MyDataContext();
                        int errorId = db.LoggedErrors.OrderByDescending(le => le.ErrorId).Select(le => le.ErrorId).FirstOrDefault();

                        Server.Transfer("~/Error.aspx?ErrorID=" + errorId); 
                    }
                }
                catch (Exception)
                {
                }
            }
like image 485
user2110845 Avatar asked Feb 14 '14 14:02

user2110845


1 Answers

As you have not posted much code. So without seeing the actual implementation you have done. I could suggest you below points.

Point 1. First of all, you need to check if SessionState is enabled for pages. You could set them globally in web.config file. Try the snippet given below in web.config

<configuration>   
   <system.web>
   <pages enableSessionState="true" />
  </system.web>
</configuration>

Point 2. And put your Redirection in Application_Error in Global.asax.

public void Application_Error(object sender, EventArgs e)
{
     HttpApplication app = (HttpApplication)sender;
     app.Server.Transfer("~/Error.aspx?ErrorID=" + errorId,true);
 }

Point 3. Also check if your SessionStateis set properly in IIS too.

Details are on MSDN to enable sessionstate

Hope this helps..!!!

like image 154
Mayank Pathak Avatar answered Oct 04 '22 02:10

Mayank Pathak