【问题标题】:What is the best way to save current language(UICulture) in asp.net mvc 5在asp.net mvc 5中保存当前语言(UICulture)的最佳方法是什么
【发布时间】:2015-02-05 05:12:07
【问题描述】:

我想根据请求的 url 设置语言。我现在正在使用 Session 执行此操作。但我无法访问 Route Constraints 中的 Session。问题出现在这里:返回错误,因为 'HttpContext.Current.Session' 在 RouteConstraint 中为空。

public class ngnRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       ...
       Tools.GetCurrentLang();// trying to get language
       ...
    }
}

在工具中:

public static string GetCurrentLang()
{
    if (HttpContext.Current.Session["Lang"] != null)
    {
        return HttpContext.Current.Session["Lang"].ToString();
    }
    else
    {
        return WebConfigurationManager.AppSettings["DefaultLang"];
    }
}  

我还尝试添加:runAllManagedModulesForAllRequests="true" 并 romove 并添加 Seesion 模块,但我遇到了同样的错误。

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="FormsAuthenticationModule" />
  </modules>
</system.webServer>  

问题: 我能做什么?我可以用 session 或 cookie 处理这个问题吗?你有什么建议?谢谢你的一切。

【问题讨论】:

    标签: asp.net asp.net-mvc session


    【解决方案1】:

    您是否尝试将 HttpContextBase 参数传递给您的 GetCurrentLang() 方法?

    public static string GetCurrentLang(HttpContextBase httpContext)
    {
        if (httpContext.Session["Lang"] != null)
        {
            return httpContext.Session["Lang"].ToString();
        }
        else
        {
            return WebConfigurationManager.AppSettings["DefaultLang"];
        }
    } 
    

    【讨论】:

    • 否,但 HttpContextBase httpContexthttpContext.Session 在 Match 函数中为空。问题是:我认为会话尚未初始化。
    猜你喜欢
    • 1970-01-01
    • 2010-09-08
    • 2015-03-07
    • 2010-12-02
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2012-04-08
    相关资源
    最近更新 更多