asp.net-mvc-4 – 部署到IIS 7后,ASP.NET MVC 4 webapplication
我开发了一个ASP.NET MVC 4 Web应用程序(.net 4.5),它在Visual Studio 2012中运行良好.在
Windows Server 2008 R2上部署到IIS 7后,我的控制器中的HttpContext.Session对象似乎为null.我创建了一个简单的测试ASP.NET MVC 4应用程序来演示该问题.
在我的测试应用程序中,我有一个简单的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视图看起来像这样: @{ ViewBag.Title = "Index"; } <h2>Index</h2> This is a simple test <p>@ViewBag.Info</p> 因此,当我运行应用程序时,我得到了我所期望的: Index This is a simple test a string in session state 但在我将应用程序部署到Web服务器之后,该网站向我提供了以下页面,指示Session对象为null: Index This is a simple test beeeeuuuuu - Session = null!!! Web应用程序部署到默认网站,该网站在ASP.NET v4.0应用程序池(集成管道)下运行. 我已经使用aspnet_regiis -ir在服务器上重新安装了ASP.NET,但这没有帮助.会话状态在ISS服务器上启用(在Proc中).我希望有人可以帮助我,因为我试图解决这个问题很长一段时间. 非常感谢提前和亲切的问候. 更新:我还测试了使用.NET 4.0而不是4.5的ASP.NET MVC 4版本,并且具有相同的问题.我还部署了一个ASP.NET网页应用程序(.NET 4.0),并且工作正常(HttpContext.Current.Session在后面的代码中不为null). 更新II:我还试图将会话状态存储在一个数据库中,该数据库在我的开发机器上工作正常,但在生产服务器上有同样的问题(HttpContext.Session仍然返回null). 解决方法
我找到了解决方案.您必须添加和删除SessionStateModule:
<configuration> ... <system.webServer> ... <modules> <remove name="Session" /> <add name="Session" type="System.Web.SessionState.SessionStateModule"/> ... </modules> </system.webServer> </configuration> 我不知道微软为什么不将它添加到项目模板的web.config中? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |