2

我有一个 asp.net mvc4 应用程序,其中有这个片段:

   public bool Logout() {
            try {
                session["user"] = null;
                return true;
            }
            catch {
                return false;
            }
                          }

当我将此代码放在控制器中时,它可以工作,但是如果我将其放在模型类中,则没有。问题出在session["user"] = null;.

那么如何在模型类中管理会话的变量呢?

4

2 回答 2

2

In class access by the current context :

HttpContext.Current.Session["user"]....
于 2013-09-16T09:37:55.023 回答
2

This functionality should not be in a view model. The model should be used for passing data from controllers to views for displaying, and receiving submitted data from views.

See a question like What is a ViewModel in MVC to get a better explanation.

A logout function should be an action on a controller. Something like:

public ActionResult Logout()
{
    Session["user"] = null;

    // Redirect user to homepage
    return Redirect("/");
}
于 2013-09-16T09:39:01.843 回答