会话状态只能在enableSessionState设置为true时使用,当我使用Server.Transfer
问题描述:
包含Error页面的网站中的每个页面都是从Master页面派生的。在主页面我正在访问会话变量。当我得到异常,在Page_Error或Application_Error事件中处理。从那里我重定向到使用Server.Transfer的错误页面,然后我在Error.aspx的母版页中得到下面的异常。如果我使用Response.Redirect它正常工作。会话状态只能在enableSessionState设置为true时使用,当我使用Server.Transfer
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>\<system.web>\<httpModules> section in the application configuration.
请详细解释Server.Transfer的问题。
答
当应用程序抛出异常时,我正在处理Application_Error事件。
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = HttpContext.Current.Server.GetLastError();
if (ex.Message == "File does not exist." && HttpContext.Current.Session == null)
{
if (((System.Web.HttpException)(ex)).GetHttpCode() == 404)
{
LogtheException();
}
}
else
{
Log the Exception(Session["uname"].ToString());
Server.Transfer(UrlMaker.ToError(ex.Message.ToString()));
}
}
使用HttpContext.Current.Server.GetLastError();我得到最后一个例外。如果有任何异常,“文件不存在”。正在访问会话变量。
首先,它会抛出相关应用程序异常,那么如果任何CSS /映像文件的路径是不正确的,然后紧接着就抛出了“文件不存在。”例外。例外是因为没有正确处理“文件不存在”的会话。案件。
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>\<system.web>\<httpModules> section in the application configuration.
现在我才知道,CSS和图像请求通常不需要访问会话,因此ASP不会话加载到内存中,你不能够访问它异常“文件不不存在“。