一般在系统中持续偶尔都会发生没有完全被捕捉或者无法处理的异常,这时候一般都会提醒用户刷新页面或者重新登录;
最近学到一个做法,发生遗产时将错误页面的url存入Session,便于用户在刷新后如果系统能够正常运行时返回原来页面:
public void ThrowException()
{
try
{
......................
}
catch
{
string AbsoluteUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;//绝对路径,跳动过来的出错页面的链接
System.Web.HttpContext.Current.Session["ErrorUrl"] = AbsoluteUrl;//存入Session,
System.Web.HttpContext.Current.Response.Redirect("~/Error.aspx");调至错误页面
}
}
在错误页面Error.aspx中有两种选择,1,刷新页面,如果能够成功则返回原来页面,2,重新登录
1,刷新按钮的方法
protected void btnRefresh_Click(object sender, ImageClickEventArgs e)
{
if (System.Web.HttpContext.Current.Session["ErrorUrl"] != null)
{
string Ablturl = System.Web.HttpContext.Current.Session["ErrorUrl"].ToString();//发生错误时存储的url
Response.Redirect(Ablturl);//重新调至原来页面
}
else
{
Response.Write("<font color='red' size='16px'>Page Error!</font>");
}
}
2,如果刷新后还是跳至错误页面,那么就需要重新登录了
/// <summary>
/// 返回登录页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Login_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");//返回主页
}