Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
957 views
in Technique[技术] by (71.8m points)

iis 7 - The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)

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();
          }

    }
}

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

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!!!

The web application is deployed to the default website which runs under the ASP.NET v4.0 application pool (integrated pipeline).

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

I have no idea why Microsoft doesn't add this to the web.config of the project template?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...