沒有路由,HttpContext.Current.Session
在那裏,所以我知道StateServer
正在工作。當我路由我的請求時,HttpContext.Current.Session
在路由頁面中是null
。我在IIS 7.0上使用.NET 3.5 sp1,沒有MVC預覽。看來AcquireRequestState
在使用路由時永遠不會被觸發,因此session變量沒有實例化/填充。HttpContext.Current.Session在路由請求時爲空
當我嘗試訪問會話變量,我得到這個錯誤:
base {System.Runtime.InteropServices.ExternalException} = {"Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>.
調試時,我也得到了錯誤的HttpContext.Current.Session
是不是在這方面進行訪問。
-
我web.config
看起來是這樣的:
<configuration>
...
<system.web>
<pages enableSessionState="true">
<controls>
...
</controls>
</pages>
...
</system.web>
<sessionState cookieless="AutoDetect" mode="StateServer" timeout="22" />
...
</configuration>
這裏的IRouteHandler實現:
public class WebPageRouteHandler : IRouteHandler, IRequiresSessionState
{
public string m_VirtualPath { get; private set; }
public bool m_CheckPhysicalUrlAccess { get; set; }
public WebPageRouteHandler(string virtualPath) : this(virtualPath, false)
{
}
public WebPageRouteHandler(string virtualPath, bool checkPhysicalUrlAccess)
{
m_VirtualPath = virtualPath;
m_CheckPhysicalUrlAccess = checkPhysicalUrlAccess;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
if (m_CheckPhysicalUrlAccess
&& !UrlAuthorizationModule.CheckUrlAccessForPrincipal(
m_VirtualPath,
requestContext.HttpContext.User,
requestContext.HttpContext.Request.HttpMethod))
{
throw new SecurityException();
}
string var = String.Empty;
foreach (var value in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[value.Key] = value.Value;
}
Page page = BuildManager.CreateInstanceFromVirtualPath(
m_VirtualPath,
typeof(Page)) as Page;// IHttpHandler;
if (page != null)
{
return page;
}
return page;
}
}
我也試圖把EnableSessionState="True"
在aspx頁面的頂部但仍然沒有。
任何見解?我應該寫另一個執行IRequiresSessionState
的HttpRequestHandler
嗎?
謝謝。
非常感謝。它很好地解決了我的問題 - 事實證明,生產服務器需要它,但不是開發機器。 – Raithlin 2010-02-03 07:42:04
這是ins @ ne!謝謝。這固定了我的「TempData」(mvc剃鬚刀)值也從消失(因爲TempData使用會話)。聖莫里。 – granadaCoder 2015-08-10 20:44:52
我有TempData值消失的問題,但這並沒有解決它。 – Dinei 2015-08-27 19:54:12