2015-02-04 43 views
1

我想根据请求的url设置语言。现在我正在使用Session进行此操作。但我无法访问路由约束中的会话。问题发生在这里:返回错误,因为'HttpContext.Current.Session'在RouteConstraint中为null。在asp.net mvc中保存当前语言(UICulture)的最佳方式是什么5

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 = “真”和移去那些所谓并添加SEESION模块,但我得到了同样的错误。

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

问题: 我能做什么呢?我可以通过会话或cookie处理这个问题吗?你有什么建议?谢谢你的一切。

回答

0

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

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

不,但'HttpContextBase httpContext'的** httpContext.Session **在Match函数中为null。问题是:我认为会话尚未初始化。 –

相关问题