在我们正在建设的网站上.我们需要能够在会话结束时将用户重定向到默认页面. 乍一看,我们使用Session_End和Response.Redirect来完成这项工作. Sub Session_End(ByVal sender As Object, ByVal e As EventArg
乍一看,我们使用Session_End和Response.Redirect来完成这项工作.
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect("~/global/exit.aspx")
End Sub
但是它会在此上下文错误中生成一个无法响应的垃圾邮件.当然,我们不想垃圾邮件我们的服务器错误日志.
使用ASP.NET 2.0处理会话结束的最有效方法是什么?
我们将以下代码添加到global.asax.cs文件中:private void IsAuthenticated()
{
string vFileName = Path.GetFileName(HttpContext.Current.Request.Path);
string vExt = Path.GetExtension(vFileName).ToLower();
if ((vFileName != "Login.aspx") && (vExt == ".aspx"))
{
if (HttpContext.Current.Session["LoggedIn"] == null)
{
HttpContext.Current.Response.Redirect("~/Login.aspx");
}
}
}
void Application_PostAcquireRequestState(object sender, EventArgs e)
{
IsAuthenticated();
}
NS:我们的Global .asax文件中的第一行是:
<%@ Application Inherits="???.Global" Language="C#" %>
