Sunday, November 6, 2011

Asp.Net Session variable not set on Redirect()

In Asp.Net, when you want to set a Session variable then immediately do a Response.Redirect(), the Session variable gets lost, like it was never set. Asp.Net has an overloaded version of  Response.Redirect() that, as well as a destination URL, it takes a bool that tells Asp.Net whether to end the thread or not.

By default, calling Response.Redirect(string Url) will terminate the response thread, meaning your session variable will not get set, only the redirect header gets sent to the client. Use something like this instead.

Session["somevariable"] = "some value";
Redirect("http://whatever..", false);
//Rest of code gets executed

No comments:

Post a Comment