Session.Clear()和Session.Abandon()都摆脱了会话变量.据我了解,Abandon()结束当前会话,并导致创建一个新会话,从而导致End和Start事件被触发.
在大多数情况下,最好调用Abandon(),例如将用户注销.有没有我使用Clear()的情况?是否存在很大的性能差异?
MVC菜鸟在这里。
当前,我有以下代码通过AJAX加载页面时会触发HomeController:
namespace ETTData.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public ContentResult clearSessions()
{
var currentSession = System.Web.HttpContext.Current.Session;
System.Diagnostics.Debug.WriteLine("BEFORE: " + currentSession.Timeout);
currentSession.Abandon();
//currentSession.RemoveAll();
//currentSession.Clear();
System.Diagnostics.Debug.WriteLine("AFTER : " + currentSession.Timeout);
return new ContentResult { Content = "OK", ContentType = "text/plain" };
}
}
}
Run Code Online (Sandbox Code Playgroud)
debug.WriteLine的输出是:
之前:35
之后:35
所以你可以看到它有35对之前还有35对后,当它不应该等于什么,因为我用currentSession.Abandon(); 在调用该输出之前。
我通过Global.asax.cs文件设置会话超时:
namespace ETTData
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Session_Start(Object sender, EventArgs …Run Code Online (Sandbox Code Playgroud)