asp.net 异常处理

protected void Application_EndRequest(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码

        HandlerError();
    }

    private void HandlerError()
    {            
        if (HttpContext.Current != null)
        {
            if (string.IsNullOrEmpty(Request.QueryString["error"]))
            {
                var server = HttpContext.Current.Server;
                var statusCode = HttpContext.Current.Response.StatusCode;

                Exception ex = server.GetLastError();

                if (ex != null || statusCode == 404 || statusCode == 500)
                {
                    server.ClearError();
                    Response.StatusCode = 404;
                    Response.Redirect("//www.baidu.cn/nopage.html");
                    Response.End();
                    if (ex != null)
                        DependencyResolver.Current.GetService<ILog>().Log(ex);
                }
            }
        }

    }

    protected void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码
        HandlerError();
    }
posted @ 2017-11-20 19:27  给我一个理由  阅读(76)  评论(0编辑  收藏