ASP.Net Session data lost between pages
I came across a weird behavior today w/ my web application. When I navigate from a page to another, I lose one particular session variable data.
I'm able to launch the app in firefox and able to see that the session data is not lost.
I use Response开发者_如何学JAVA.Redirect(page2, false)
to redirect to another page.
Below code was used to track session variables
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"c:\test.txt", true);
for (int i = 0; i < Session.Count; i++)
{
sw.WriteLine(Session.Keys[i] + " " + Session.Contents[i]);
}
sw.Close();
Can anyone help me in this? Any help is appreciated.
I was having exactly the same problem and in my case I found the cause of this behavior. It turned out to be that when I was invoking the Response.Redirect() method I was using the full url instead of just the page name. So when I was in localhost/myapp/page1.aspx I redirected to MYMACHINENAME/myapp/page2.aspx and that's why the sessions were different for each page. I corrected this in my code using only "page2.aspx" and then the final url on any browser (IE, firefox) was localhost/myapp/page2.aspx.Don't know if you're playing with the urls the way I was doing it but maybe this answer can give you a clue. Thanks and good coding
Are you developing in a web farm / web garden environment?
Try using the state server mode. Depending on how your application pool is configured and your deployments the default in-process mode can be unpredictable.
My problem was as follows :-
Problem: When we have moved the ASP.NET application to an another server (Windows Server 2008 R2) with IIS 7.5, the application cannot move session values between the pages. e.g. the session value was set in first page but it could not move to next page. In next page, value for same session variable was coming NULL.
Session values was moving to next page in case of Google Chrome and Firefox but not in Internet Explorer.
Resolution: We have created URL name with "_" (underscore) e.g. http://MySite_test.com. After removing "_", it works as required e.g. http://MySitetest.com
Other Possible Solution:
Use Response.Redirect with having second parameter as "false" to avoid execution of page and thus to avoid lose session token. You have to use URL as follows. Response.Redirect("NextPage.aspx",false)
If the application pool of the site is configured as a web farm or a web garden (by setting the maximum number of worker processes to more than one), and if you're not using the session service or SQL sessions, incoming requests will unpredictably go to one of the worker processes, and if it's not the one the session was created on, it's lost. The solutions to this problem is either not to use a web garden if you don't need the performance boost, or use one of the out of process session providers.
精彩评论