ASP.NET MVC - 会话为null [英] ASP.NET MVC - Session is null
问题描述
我有它的会话在开发环境中工作,而不是在生产上一个.NET4应用MVC3。结果
在生产记录我的SessionID和它的那一刻我和设置从会话获取相同的。结果
当我试图让我越来越空异常
会话。结果
这是我访问会话:
I have an MVC3 application on .net4 that its session working in the dev Environment, but not in the production.
In the production I logged the sessionID and the it is the same in the moment I Set and Get from the session.
When I try to get the session I am getting Null Exception
.
This is how I access the session:
public static class HandlersHttpStorage
{
public static string TestSession
{
get
{
return HttpContext.Current.Session["time"];//This is null
}
set
{
HttpContext.Current.Session.Add("time", value);//DateTime.Now.ToString()
}
}
}
什么是令我担心的是,在生产的行为比发展的不同,即使在web.config是一样的。
What's makes me worried is that the behavior in the production is different than the development, even though the web.config is the same.
感谢
推荐答案
链接: HttpContext.Current.Session为空时,将请求路由
明白了。很愚蠢,其实。它的工作后我删除和放大器;添加SessionStateModule像这样:
Got it. Quite stupid, actually. It worked after I removed & added the SessionStateModule like so:
<configuration>
...
<system.webServer>
...
<modules>
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
...
</modules>
</system.webServer>
</configuration>
简单地增加,因为会话它不会工作应该已经定义的的machine.config
。
现在,我不知道这是做平常的事情。它肯定似乎不那么因为看起来那么粗...
Now, I wonder if that is the usual thing to do. It surely doesn't seem so since it seems so crude...
链接: HttpContext.Current.Session 空项
sessionKey可能会改变,你可能只需要做的:
sessionKey may be changing, you probably only need to do:
HttpContext.Current.Session["CurrentUser"]
或会话可能会过期,检查超时:
Or the session may be expiring, check the timeout:
<一个href=\"http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx\">http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx
或者你也可以从别的地方设置会话值,通常我控制通过一个属性访问会话/上下文对象
Or you may be setting the session value from somewhere else, normally i control access to Session/Context object through one property
static readonly string SESSION_CurrentUser = "CurrentUser";
public static SiteUser Create() {
SiteUser.Current = new SiteUser();
return SiteUser.Current;
}
public static SiteUser Current {
get {
if (HttpContext.Current.Session == null || HttpContext.Current.Session[SESSION_CurrentUser] == null) {
throw new SiteUserAutorizationExeption();
}
return HttpContext.Current.Session[SESSION_CurrentUser] as SiteUser;
}
set {
if (!HttpContext.Current.Session == null) {
HttpContext.Current.Session[SESSION_CurrentUser] = value;
}
}
}
这篇关于ASP.NET MVC - 会话为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!