HttpContext.Current.Response.Redirect("~/default.aspx");
,当我在Session_End中Global.asax中 错误使用此代码对我说:重定向从Global.asax中
Object reference not set to an instance of an object.
为什么?
HttpContext.Current.Response.Redirect("~/default.aspx");
,当我在Session_End中Global.asax中 错误使用此代码对我说:重定向从Global.asax中
Object reference not set to an instance of an object.
为什么?
Session_End中不是一个事件它被应用程序的用户调用,当会话超时时,服务器会调用它。所以当你试图访问HttpContext时,它是空的,因为没有HttpContext来访问(没有用户正在与你的站点进行某种交互)。
无论你做什么,试图重定向一个不存在的HttpContext都会失败。
现在我在做什么重定向session_end? – Ali
在Session_End中重定向是没有意义的,因为没有什么可以重定向。 Session_End事件是当会话超时,没有用户重定向。这只是服务器端。 –
当事件SessionEnd被提出时,请求然后响应为空。
HttpContext.Current.Response //this is null
这是故意的:会议要求中没有结束,但是当会话超时
它通常(默认配置)上次请求之后会发生20分钟。
由于没有请求也没有响应。
You need to understand better how Asp.net Session State works
无论如何,如果你想如果会话已过期,您可以检查存储在会议的变量之一,将用户重定向到一个页面:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["YOUR_VAR_NAME"]==null)
{
Response.Redirect("~/default.aspx");
}
}
Session_End由服务器根据内部计时器在内部触发。因此,当发生这种情况时,没有HttpRequest相关联。这就是为什么Response.Redirect或Server.Transfer没有意义并且不起作用的原因。
我希望上述信息将有助于
的可能重复[如何处理在Global.asax中会话结束?(http://stackoverflow.com/questions/621744/how-to-handle-session -end-in-global-asax) – Win