Session对象是ASP.NET MVC 4 web应用一旦部署到IIS 7(W 2008 R2)空 [英] The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)
问题描述
我开发了运行在Visual Studio 2012精部署IIS 7的Windows Server 2008 R2上后的ASP.NET MVC 4 Web应用程序(.NET 4.5),它似乎像HttpContext.Session对象内我的控制器为null 。我创建了一个简单的测试ASP.NET MVC 4应用程序来演示这个问题。
I developed an ASP.NET MVC 4 web application (.net 4.5) which runs fine in Visual Studio 2012. After deployment to IIS 7 on Windows Server 2008 R2, it seems like the HttpContext.Session object inside my controller is null. I created a simple test ASP.NET MVC 4 application to demonstrate the issue.
在我的测试程序我有一个简单的家庭控制器
In my test app I have a simple Home Controller
public class HomeController : Controller
{
public ActionResult Index()
{
if ( HttpContext != null && HttpContext.Session != null )
{
HttpContext.Session[ "test" ] = "a string in session state";
ViewBag.Info = HttpContext.Session[ "test" ];
return View();
}
else
{
if ( HttpContext == null )
{
ViewBag.Info = "beeeeuuuuu - HttpContext = null!!!";
}
else if ( HttpContext.Session == null )
{
ViewBag.Info = "beeeeuuuuu - Session = null!!!";
}
return View();
}
}
}
我Index.chtml观点lookes是这样的:
My Index.chtml view lookes something like this:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
This is a simple test
<p>@ViewBag.Info</p>
所以,当我运行该应用程序我得到了我所期待的:
So when I run the application I get what I was expecting:
Index
This is a simple test
a string in session state
但是在我将应用程序部署到Web服务器的网站给我,表明Session对象为null以下页面:
But after I deploy the application to the web server the website gives me the following page indicating that the Session object is null:
Index
This is a simple test
beeeeuuuuu - Session = null!!!
Web应用程序部署到该ASP.NET v4.0的应用程序池(综合管线)下运行的默认网站。
The web application is deployed to the default website which runs under the ASP.NET v4.0 application pool (integrated pipeline).
我已经重新安装ASP.NET使用-ir aspnet_regiis的服务器上,但是这并没有帮助。会话状态是在ISS服务器启用(在PROC)。我希望有人能帮助我在这里的原因,我想解决这个问题相当长的一段时间。
I already reïnstalled ASP.NET on the server using aspnet_regiis -ir but this didn't help. The Session state is enabled (In Proc) at the ISS server. I hope anyone can help me out here cause I'm trying to solve this for quite some time.
提前和亲切的问候,不胜感谢。
Much thanks in advance and kind regards.
更新:我还与.NET 4.0而不是4.5测试了一个ASP.NET MVC 4构建和有同样的问题。我还部署了一个ASP.NET网页应用程序(.NET 4.0),且正常工作(该HttpContext.Current.Session不在code后面空)。
UPDATE: I also tested an ASP.NET MVC 4 build with .NET 4.0 instead of 4.5 and that has the same problem. I also deployed an ASP.NET Web Pages app (.NET 4.0) and that works fine (The HttpContext.Current.Session is not null in the code behind).
更新二:我也试图存储会话状态的数据库中,它工作得很好我的开发计算机上,但必须在生产服务器上同样的问题(HttpContext.Session仍然返回null)
UPDATE II: I also tried to store the Session State in a database, which worked fine on my development machine but had the same problem on the production server (HttpContext.Session still returns null).
推荐答案
我找到了解决办法。你必须添加和删除SessionStateModule:
I found the solution. You have to add and remove the SessionStateModule:
<configuration>
...
<system.webServer>
...
<modules>
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
...
</modules>
</system.webServer>
</configuration>
我不知道为什么微软不将其添加到项目模板的web.config文件?
I have no idea why Microsoft doesn't add this to the web.config of the project template?
这篇关于Session对象是ASP.NET MVC 4 web应用一旦部署到IIS 7(W 2008 R2)空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!