.net跳到指定错误页面兵显示详细的错误信息

来源:互联网 发布:淘宝网女童公主裙 编辑:程序博客网 时间:2024/05/17 03:27

1.需要在 Global.asax页面中的Application_Error事件中添加处理方法

void Application_Error(object sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码
        HttpContext ctx = HttpContext.Current;
        Exception exception = ctx.Server.GetLastError();
        string directUrl = "DefaultError.html?error=";
        if (exception != null)
        {
            string errorInfo = "URL:" + ctx.Request.RawUrl.ToString() + "Source:" + exception.Source
            + "Message:<strong>" + exception.Message + "</strong>";
            errorInfo = "错误信息为:<strong>" + exception.InnerException.Message + "</strong>";
            //if (exception is HttpException)
            //{
            //    HttpException ex = exception as HttpException;
            //    int httpCode = ex.GetHttpCode();
            //    errorInfo = "Code:<strong>" + httpCode.ToString() + "</strong>" + errorInfo;
            string url=ctx.Request.Url.ToString();
            directUrl = url.Substring(0, url.ToString().IndexOf("/Web/") + 5) + 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);
    }

2.在错误页中获取错误信息并显示出来