【问题标题】:Strongly Type Session Variables in MVC 3MVC 3 中的强类型会话变量
【发布时间】:2012-06-28 18:29:06
【问题描述】:

我正在尝试在 MVC 3 应用程序中设置会话包装器。这篇文章给出了我想做的大致想法:http://weblogs.asp.net/cstewart/archive/2008/01/09/strongly-typed-session-in-asp-net.aspx

但是会话在被访问时始终为空。这个类在控制器之外,目前,正在控制器之外访问有问题的变量。

这是课程:

public sealed class SessionManager : IRequiresSessionState
{
    private const string CurrentCultureKey = "CurrentCulture";
    public static Culture CurrentCulture
    {
        get
        {
            if (null != HttpContext.Current.Session[CurrentCultureKey])
                return (Culture)HttpContext.Current.Session[CurrentCultureKey];

            return Culture.En;
        }
        set
        {
            if (HttpContext.Current.Session != null)
            HttpContext.Current.Session[CurrentCultureKey] = value;
        }
    }
}

我添加了 IRequiresSessionState,但这似乎也不起作用。

我正在使用此变量来帮助自定义路由,以防我想在会话中跟踪它的地址 (/En/Controller/Action) 中的区域性。这对自定义错误视图很有帮助。

我感觉自己走错了路,所以如果有人能指出我正确的方向,我将不胜感激。

【问题讨论】:

    标签: asp.net-mvc session variables


    【解决方案1】:

    所以,最后我没有找到在控制器操作方法之外访问会话的方法。相反,我将值存储在 cookie 中,这更有意义。

    但是,如果用户关闭了 cookie,并且网站上发生了错误,目前它们将被转发到默认语言。我可能必须将此信息存储在数据库中,或者在没有 cookie 的情况下存储具有唯一 ID 的缓存项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多