首页 文章

Session 对象在 asp mvc 中总是返回 null?

提问于
浏览
0

我正在使用 asp mvc 开发一个网站,但我遇到了 Session 对象的问题。

我有这个类来管理会话,有一个存储语言的属性:

公共类 SessionManager {

public static string Language
    {
        get
        {              

           object language = GetSession("LANGUAGE");

            if (language != null)
            {
                return language as string;
            }
            return "EN";

        }
        set
        {               
            SetSession("LANGUAGE", value);     
        }
    }

    //methods for creating session objects

    private static bool SetSession(string key, object value)
    {
        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            HttpContext.Current.Session[key] = value;
            return true;
        }
        return false;
    }

    private static object GetSession(string key)
    {
        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            return HttpContext.Current.Session[key];
        }
        return null;
    }

}

当我想更改语言时,我调用 HomeController 中设置新语言的方法:

public  void SetLanguage(string language)
    {                    
        SessionManager.Language = language;

    }

但问题是 Session 没有存储新值,它总是在 SessionManager 类中返回 null。为什么它是 null?请帮忙!

1 回答

  • 0

    静态类和静态实例字段在 asp.net mvc 中的所有请求之间共享。每个用户的会话数应该是唯一的。这意味着你应该避免在会话范围内使用 static关键字。

    还要记住,在回收应用程序池之前,不会从内存中清除静态实例。这可能导致内存使用问题。

    我已经尝试了你的代码并找出了工作方法。重写了 SessionsManager 示例

    public class SessionManager
    {
        private HttpContextBase _context;
    
        public SessionManager(HttpContextBase context)
        {
            _context = context;
        }
    
        public string Language
        {
            get
            {
    
                object language = GetSession("LANGUAGE");
    
                if (language != null)
                {
                    return language as string;
                }
                return "EN";
    
            }
            set
            {
                SetSession("LANGUAGE", value);
            }
        }
    
        private bool SetSession(string key, object value)
        {
            if (_context != null && _context.Session != null)
            {
                _context.Session[key] = value;
                return true;
            }
            return false;
        }
    
        private object GetSession(string key)
        {
            if (_context != null && _context.Session != null)
            {
                return _context.Session[key];
            }
            return null;
        }
    }
    

    您可以通过这种方式使用您的经理

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var sm = new SessionManager(HttpContext);
            ViewBag.Language = sm.Language;
            return View();
        }
    
        [HttpGet]
        public ActionResult SetRu()
        {
            var sm = new SessionManager(HttpContext);
            sm.Language = "Russian";
            return RedirectToAction("Index");
        }
    
        [HttpGet]
        public ActionResult SetUa()
        {            
            var sm = new SessionManager(HttpContext);
            sm.Language = "Ukrainian";
            return RedirectToAction("Index");
        }
    }
    

    此代码工作正常,可满足您的需求

相关问题