private void OnListenerErrorPageRequested(object sender, ErrorPageEventArgs e) { _server = this; DisplayErrorPage(e.Context, e.Exception); e.IsHandled = true; }
/// <summary> /// 发送异常页面通知 /// </summary> /// <param name="exception">异常</param> private void SendErrorPage(Exception exception) { var httpException = exception as HttpException; var response = HttpContext.Current.Response; response.Status = httpException != null ? httpException.Code : HttpStatusCode.InternalServerError; response.Reason = exception.Message; if (response.Body.CanWrite) { response.Body.SetLength(0); } var args = new ErrorPageEventArgs(HttpContext.Current) { Exception = exception }; // 发送异常错误页面请求事件通知 ErrorPageRequested(this, args); try { var generator = new ResponseWriter(); if (args.IsHandled) { generator.Send(HttpContext.Current, response); } else { generator.SendErrorPage(HttpContext.Current, response, exception); } } catch (Exception err) { Logger.Error("Failed to display error page"); ExceptionHandler.Handle(err); } }
/// <summary> /// 有错误发生,发送错误显示页面至客户端 /// </summary> /// <param name="context">The context.</param> /// <param name="exception">The exception.</param> /// <remarks> /// Invoke base class (<see cref="Server"/>) to send the contents /// of <see cref="IHttpContext.Response"/>. /// </remarks> protected virtual void DisplayErrorPage(IHttpContext context, Exception exception) { var httpException = exception as HttpException; if (httpException != null) { context.Response.Reason = httpException.Code.ToString(); context.Response.Status = httpException.Code; } else { context.Response.Reason = "Internal Server Error"; context.Response.Status = HttpStatusCode.InternalServerError; } var args = new ErrorPageEventArgs(context) { Exception = exception }; ErrorPageRequested(this, args); if (args.IsHandled) { // 外部已处理 var writer = HttpFactory.Current.Get <ResponseWriter>(); writer.Send(context, context.Response); } else { // 外部未处理,发送异常内容 var writer = HttpFactory.Current.Get <ResponseWriter>(); writer.SendErrorPage(context, context.Response, exception); args.IsHandled = true; } }
/// <summary> /// 发送异常页面通知 /// </summary> /// <param name="exception">异常</param> private void SendErrorPage(Exception exception) { var httpException = exception as HttpException; var response = HttpContext.Current.Response; response.Status = httpException != null ? httpException.Code : HttpStatusCode.InternalServerError; response.Reason = exception.Message; if (response.Body.CanWrite) response.Body.SetLength(0); var args = new ErrorPageEventArgs(HttpContext.Current) { Exception = exception }; // 发送异常错误页面请求事件通知 ErrorPageRequested(this, args); try { var generator = new ResponseWriter(); if (args.IsHandled) generator.Send(HttpContext.Current, response); else generator.SendErrorPage(HttpContext.Current, response, exception); } catch (Exception err) { Logger.Error("Failed to display error page"); ExceptionHandler.Handle(err); } }
/// <summary> /// 有错误发生,发送错误显示页面至客户端 /// </summary> /// <param name="context">The context.</param> /// <param name="exception">The exception.</param> /// <remarks> /// Invoke base class (<see cref="Server"/>) to send the contents /// of <see cref="IHttpContext.Response"/>. /// </remarks> protected virtual void DisplayErrorPage(IHttpContext context, Exception exception) { var httpException = exception as HttpException; if (httpException != null) { context.Response.Reason = httpException.Code.ToString(); context.Response.Status = httpException.Code; } else { context.Response.Reason = "Internal Server Error"; context.Response.Status = HttpStatusCode.InternalServerError; } var args = new ErrorPageEventArgs(context) { Exception = exception }; ErrorPageRequested(this, args); if (args.IsHandled) { // 外部已处理 var writer = HttpFactory.Current.Get<ResponseWriter>(); writer.Send(context, context.Response); } else { // 外部未处理,发送异常内容 var writer = HttpFactory.Current.Get<ResponseWriter>(); writer.SendErrorPage(context, context.Response, exception); args.IsHandled = true; } }