问题
I have an application in MVC4 which has some logic in the Session_End()
method in Global.aspx.cs
.
However, I need to change the session state from InProc
to SQLServer
, and this means the Session_End()
method will not get called.
I've tried to effect a workaround but I couldn't get it to work.
What are the alternatives, if any, to Session_End()
? All I need is the ability to run some logic when the session times out.
回答1:
The session doesn't time out when SessionState
is in SQLServer
mode.
From the MSDN page on Session-State Modes:
SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.
回答2:
This is just a guess, but if the Session has ended, the Session property will be null or empty?
Perhaps you could test for that, maybe in a BaseController or something which is called for each request.
来源:https://stackoverflow.com/questions/25543983/session-end-alternatives-for-sql-server-session-state