一旦部署到 IIS 7 (W 2008 R2), ASP.NET MVC 4 web 应用程序中的 Session 对象为空 [英] The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)

查看:34
本文介绍了一旦部署到 IIS 7 (W 2008 R2), ASP.NET MVC 4 web 应用程序中的 Session 对象为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个在 Visual Studio 2012 中运行良好的 ASP.NET MVC 4 Web 应用程序 (.net 4.5).在 Windows Server 2008 R2 上部署到 IIS 7 后,我的控制器内的 HttpContext.Session 对象似乎为空.我创建了一个简单的测试 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.

在我的测试应用中,我有一个简单的 Home Controller

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 视图如下所示:

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 对象为空:

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).

我已经使用 aspnet_regiis -ir 在服务器上重新安装了 ASP.NET,但这并没有帮助.会话状态在 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 不为空).

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?

这篇关于一旦部署到 IIS 7 (W 2008 R2), ASP.NET MVC 4 web 应用程序中的 Session 对象为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆