【发布时间】: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