相关疑难解决方法(0)

在ASP.NET中,何时应该使用Session.Clear()而不是Session.Abandon()?

Session.Clear()和Session.Abandon()都摆脱了会话变量.据我了解,Abandon()结束当前会话,并导致创建一个新会话,从而导致End和Start事件被触发.

在大多数情况下,最好调用Abandon(),例如将用户注销.有没有我使用Clear()的情况?是否存在很大的性能差异?

.net asp.net session session-state

113
推荐指数
3
解决办法
11万
查看次数

调用Abandon()后未清除ASP.net MVC 5会话

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)

c# asp.net model-view-controller asp.net-mvc asp.net-mvc-5

5
推荐指数
2
解决办法
857
查看次数