protected void onError(object sender, System.EventArgs e) { System.Web.HttpContext context = System.Web.HttpContext.Current; System.Web.HttpException tempError = context.Server.GetLastError() as System.Web.HttpException; if (tempError != null && tempError.GetHttpCode() == 404) { // this.saveLog(context); //' R.L.: Ignores costum error property due to split 404 and error handling. if (context.Request.RawUrl.Contains(".aspx")) { /* context.Response.StatusCode = 404; Configuration.CustomErrorsSection tempConfig = (Configuration.CustomErrorsSection)Configuration.WebConfigurationManager.GetSection("system.web/customErrors"); string tempFile = string.IsNullOrEmpty(tempConfig.DefaultRedirect) ? "~/noavailability.aspx" : tempConfig.DefaultRedirect; if (tempConfig.Errors("404") != null) tempFile = tempConfig.Errors("404").Redirect; if (!tempFile.Contains("?")) tempFile = tempFile + "?404=true"; tempFile += "&aspxerrorpath=" + context.Request.RawUrl.Split('?')(0); if (!this.queryString(context).Equals(string.Empty)) { tempFile += "&" + this.queryString(context); } try { context.Server.ClearError(); context.Server.Transfer(tempFile); } catch (Exception tempException) { context.Response.Write(tempException.Message + ": " + tempException.StackTrace); context.Response.Flush(); context.Response.End(); } */ } } }
/// <summary> /// 输出异常内容 /// </summary> /// <param name="context"></param> public virtual void WriteException(HttpContext context) { System.Web.HttpException httpException = this.Exception as System.Web.HttpException; if (httpException != null) { context.Response.StatusCode = httpException.GetHttpCode(); context.Response.ContentType = ResponseContentType.Text; context.Response.Write(httpException.Message); } else { context.Response.StatusCode = 500; context.Response.ContentType = ResponseContentType.Text; // 将错误消息写入响应流 context.Response.Write(this.Exception.ToString()); } }
/// <summary> /// /// </summary> protected void Application_Error() { System.Exception exception = Server.GetLastError(); Response.Clear(); if (exception != null) { using (HorizonContext db = new HorizonContext()) { if (!Horizon.Web.Helpers.DatabaseHelper.IsDown(db)) { //var notification = new Horizon.Web.Models.Notification() //{ // Id = Guid.NewGuid(), // Content = exception.Message, // CreateDate = DateTime.Now, // Name = string.Format("Error {0}", (exception as System.Web.HttpException).GetHttpCode()), // Read = false, // Title = string.Format("Error {0}", (exception as System.Web.HttpException).GetHttpCode()), // Users = db.User.Where(u => u.Roles.Any(r => r.Name.Contains("SuperAdmin"))).ToList(), // Priority = 0, // Status = db.NotificationStatus.Where(n => n.Name.Equals("Unread")).SingleOrDefault() //}; //db.Notification.Add(notification); //db.SaveChanges(); } } System.Web.HttpException httpException = exception as System.Web.HttpException; if (httpException != null) { // clear error on server Server.ClearError(); Response.Redirect(string.Format("/{0}/Error/{1}", Helpers.CultureHelper.GetCurrentCulture, httpException.GetHttpCode()), true); } } }
public WebServerResponse(System.Web.HttpException httpException) { StatusCode = httpException.GetHttpCode().ToString() + " " + httpException.Message; }
/* * Parse the contents of the string, and catch exceptions */ internal void ParseString(string text, VirtualPath virtualPath, Encoding fileEncoding) { System.Web.Util.Debug.Trace("Template", "Starting parse at " + DateTime.Now); // Save the previous base dirs and line number VirtualPath prevVirtualPath = CurrentVirtualPath; int prevLineNumber = _lineNumber; // Set the new current base dirs and line number CurrentVirtualPath = virtualPath; _lineNumber = 1; // Always ignore the spaces at the beginning of a string flags[ignoreNextSpaceString] = true; try { ParseStringInternal(text, fileEncoding); // If there are parser errors caught in the parser if (HasParserErrors) { ParserError firstError = ParserErrors[0]; Exception originalException = firstError.Exception; // Use the first error as the inner exception if not already caught one. if (originalException == null) { originalException = new HttpException(firstError.ErrorText); } // Make it a HttpParseException with proper info. HttpParseException ex = new HttpParseException(firstError.ErrorText, originalException, firstError.VirtualPath, Text, firstError.Line); // Add the rest of the errors for (int i = 1; i < ParserErrors.Count; i++) { ex.ParserErrors.Add(ParserErrors[i]); } // throw the new exception throw ex; } // Make sure that if any code calls ProcessError/ProcessException after this point, // it throws the error right away, since we won't look at ParserErrors/_firstParseException // anymore ThrowOnFirstParseError = true; } catch (Exception e) { ErrorFormatter errorFormatter = null; PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_PRE_PROCESSING); PerfCounters.IncrementCounter(AppPerfCounter.ERRORS_TOTAL); // Check if the exception has a formatter errorFormatter = HttpException.GetErrorFormatter(e); // If it doesn't, throw a parse exception if (errorFormatter == null) { throw new HttpParseException(e.Message, e, CurrentVirtualPath, text, _lineNumber); } else { // Otherwise, just rethrow it throw; } } finally { // Restore the previous base dirs and line number CurrentVirtualPath = prevVirtualPath; _lineNumber = prevLineNumber; } System.Web.Util.Debug.Trace("Template", "Ending parse at " + DateTime.Now); }
public CustomHttpResponse(System.Web.HttpException httpException) { _statusCode = httpException.GetHttpCode().ToString() + " " + httpException.Message; }