我開發了一個在Visual Studio 2012中正常運行的ASP.NET MVC 4 Web應用程序(.net 4.5)。部署到Windows Server 2008 R2上的IIS 7,看起來像我的控制器中的HttpContext.Session對象爲空。我創建了一個簡單的測試ASP.NET MVC 4應用程序來演示這個問題。一旦部署到IIS 7(W 2008 R2),ASP.NET MVC 4 web應用程序中的Session對象爲空
在我的測試應用程序,我有一個簡單的家庭控制器
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是這樣的:
@{
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服務器後,網站給我以下頁面,指示會話對象爲空:
Index
This is a simple test
beeeeuuuuu - Session = null!!!
Web應用程序部署到該ASP.NET v4.0的應用程序池(綜合管線)下運行的默認網站。
我已經在服務器上使用aspnet_regiis -ir重新安裝了ASP.NET,但這並沒有幫助。會話狀態在ISS服務器上啓用(在Proc中)。我希望任何人都可以幫助我,因爲我試圖解決這個問題很長一段時間。
非常感謝提前和親切的問候。
更新:我還測試了與ASP.NET 4.0而不是4.5的ASP.NET MVC 4構建,並且具有相同的問題。我還部署了一個ASP.NET Web Pages應用程序(.NET 4.0),並且工作正常(後面的代碼中的HttpContext.Current.Session不爲null)。
更新II:我也嘗試將會話狀態存儲在數據庫中,該數據庫在我的開發機器上工作正常,但生產服務器上的問題相同(HttpContext.Session仍然返回null)。
是否在iis中禁用會話狀態? –
在IIS上沒有啓用會話狀態,並設置爲InProc會話狀態。 – Gert