Using session management to resolve session loss issues steps
1. Enter the command in CMD
CD C:\Windows\Microsoft.NET\Framework\v4. 0.30319
Re-execute
Syntax: aspnet_regsql.exe-s server ip-u Database login-p database password-D to put the database-ssadd-sstype C
Example: Aspnet_regsql.exe-s localhost-u sa-p 123456-d zsz-ssadd-sstype C
The result of successful execution is:
2. Check the database
Two tables are automatically added.
3. Adding code in Web. config
<sqlconnectionstring= "server=.; database=zsz;uid=sa;pwd=123456 " allowcustomsqldatabase=" true " mode=" SQL Server " timeout="></sessionstate >
Test
Create a new HomeController to test from show () and Write (), one to show content from session and the other to insert content in session.
PublicActionResult Show () {if(session["name"] !=NULL) { returnContent (session["name"]. ToString ()); } returnContent ("NULL"); } PublicActionResult Write () {if(session["name"] ==NULL) {session["name"] ="Wen"; } returnContent ("OK"); }
After executing the Http://localhost:5451/home/write method
Observing database table changes
Now restart the debugger, open http://localhost:5451/home/show, you will find no need to re-assign the Session.
"Note" Do not close the browser while debugging.
Precautions
1. Do not directly double-click the use of Aspnet_regsql.exe to create, to directly use the steps in the command to generate, otherwise invalid.
If the operation is done through a visual interface, the resulting result is (see), inconsistent with the results of the command line generation.
2. If the validation session is successful, do not close your browser and restart debugging. Because the SessionId in the server and the cookie in the browser are correlated, once we close the browser, the information in the cookie may be emptied by default, and when opened again, the connection is re-established and the SESSIONID is reassigned. In other words, the previously saved SessionId has been invalidated and needs to be re-assigned to the Session.
[ASP] Session management is used to resolve session loss.