A recent project found that the Session will be lost after Response. Redirect. After two days, the problem was finally found.
Problem code
Session ["xxx"] = xxx; Response. Redirect ("yyy. aspx "); |
When the page jumps to yyy. aspx and the Session is lost, Access Session ["xxx"] To get null.
Cause: When asp.net executes Response. during Redirect, the current Response is forcibly terminated. Instead of sending the cookie on the current page to the browser, a command is sent to tell the browser to resend a new HTTP request to the new URL, as a result, the current Session is lost.
To solve this problem, set the Session when receiving the Get request on the current page, so that the browser will record the current Session Id. When the browser sends a Post request to the current page, the Server does not need to send the Session ID cookie to the browser.