global中攔截404錯誤的實現方法
阿新 • • 發佈:2018-09-17
ans ref nsf 當前 redirect 處理 find code message
void Application_Error(object sender, EventArgs e) { if(Context != null) { HttpContext ctx = HttpContext.Current; Exception ex = ctx.Server.GetLastError(); HttpException ev = ex as HttpException; if(ev!= null) { if(ev.GetHttpCode() == 404) { ctx.ClearError(); Response.Redirect("~/nofound.aspx", false); Response.End(); } else { Server.Transfer("~/Error.aspx", false); } } } }
//全站 Error 處理 protected void Application_Error() { //獲取關於當前請求的 HTTP 特定信息。 if (Context != null) { Exception ex= HttpContext.Current.Server.GetLastError() as Exception; //HttpException ex = Context.Server.GetLastError() as HttpException; if (ex != null) { //404 if (ex is HttpException) { HttpException hEx= ex as HttpException; if (hEx.GetHttpCode() == 404) { Context.ClearError(); Context.Response.Redirect("~/RouteOne/NotFind/?from=" + Context.Request.UrlReferrer); Context.Response.End(); } } else { //服務器錯誤 //Context.Server.Transfer("~/RouteOne/Error/?msg=" + ex.Message); Context.Response.Redirect("~/RouteOne/Error/?msg=" + Context.Request.UrlReferrer); Context.Response.End(); } } } }
global中攔截404錯誤的實現方法