错误处理机制代码,方法让所有报错页面都指向一个页面

错误处理机制 代码 

 protected void Application_Error(object sender, EventArgs e)
        {
            HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError();
           // Exception exception_s = Server.GetLastError();
            //HttpException ex = exception_s as HttpException;
            string directUrl = "Error.aspx?error=";
            if (exception != null)
            {
                string m = exception.Message;

                string errorInfo = "URL:<strong>" + ctx.Request.RawUrl.ToString() + "<strong><br/>Source:<strong>" + exception.Source
                + "<strong><br/>Message:<strong>" + exception.Message + "<>";
                if (!m.Contains("不存在"))
                {
                    errorInfo += "<br/>错误信息为:<strong>" + exception.InnerException.Message + "<>";
                }
                if (exception is HttpException)
                {
                    HttpException ex = exception as HttpException;
                    int httpCode = ex.GetHttpCode();
                    errorInfo += "<br />Code:<strong>" + httpCode.ToString() + "<>";
                }
                string url = ctx.Request.Url.ToString();
                string suburl = url.Substring(0, url.ToString().IndexOf("/Admin/") + 1);
                directUrl = suburl + directUrl + errorInfo;
                //directUrl += "&referurl=" + ctx.Request.Url.ToString();
                //if (httpCode == 404)
                //{
                //    directUrl = "~/DefaultError.html?error=" + errorInfo;
                //}
                //if (httpCode == 403 || httpCode == 402 || httpCode == 401)
                //{
                //    directUrl = "~/DefaultError.html?error=" + errorInfo;
                //}
                //}
                ctx.Items.Add("LastError", errorInfo);
                ctx.Server.ClearError();
            }
            try
            {
                ctx.Response.Redirect(directUrl);
            }
            catch
            { }
            //ctx.Server.Transfer(directUrl);
        }
错误处理机制 代码, 要的赶紧粘贴到记事本
就写一个方法,页面所有报错 都指向一个页面就OK了

这个方法 必须写在 Global.asax 文件中,放在根目录

 

你可能感兴趣的:(错误处理机制代码,方法让所有报错页面都指向一个页面)