protected void Application_EndRequest() { bool error = false; var rd = new RouteData(); rd.DataTokens["area"] = "Admin"; // In case controller is in another area rd.Values["controller"] = "Errors"; switch (Context.Response.StatusCode) { case 404: rd.Values["action"] = "NotFound"; error = true; break; case 500: rd.Values["action"] = "ServerError"; error = true; break; } if (error) { Response.Clear(); IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } }
public void ErrorsController_Post_Roundtrip() { var errorReport = new ErrorReport { AppId = "apps/1", Category = "category", Detail = "detail", Message = "message", Source = "source", ReportedTimestampUtc = DateTime.UtcNow, UserEmail = "*****@*****.**" }; var mockErrorService = new Mock <IErrorService>(); mockErrorService .Setup( s => s.ReportError(It.IsAny <ErrorReport>(), "api-key")) .Returns(errorReport); var controller = new ErrorsController(mockErrorService.Object); WebApiControllerHelper.MakeTestable(controller, "errors"); var response = controller.Post(errorReport, "api-key"); var responseErrorTask = response.Content.ReadAsAsync <ErrorReport>(); responseErrorTask.Wait(); var responseError = responseErrorTask.Result; Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.AreEqual("apps/1", responseError.AppId); }
public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext) { HttpActionDescriptor decriptor; try { decriptor = base.SelectAction(controllerContext); } catch (HttpResponseException e) { if (e.Response.StatusCode != HttpStatusCode.NotFound && e.Response.StatusCode != HttpStatusCode.MethodNotAllowed) { throw; } controllerContext.RouteData.Values.Clear(); IHttpController errorsController = new ErrorsController(); controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "Errors", errorsController.GetType()); controllerContext.RouteData.Values["action"] = "Handle404"; controllerContext.Controller = errorsController; decriptor = base.SelectAction(controllerContext); } return(decriptor); }
//TRATATIAS DE ERROS protected void Application_Error(object sender, EventArgs e) { var app = (MvcApplication)sender; var context = app.Context; var ex = app.Server.GetLastError(); context.Response.Clear(); context.ClearError(); HttpException httpException = ExceptionMethod(ex); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["exception"] = ex; routeData.Values["action"] = "DefaultError"; if (httpException != null) { switch (httpException.GetHttpCode()) { case 404: routeData.Values["action"] = "NotFound404"; break; case 500: routeData.Values["action"] = "ServerError500"; break; } } IController controller = new ErrorsController(); controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData)); }
protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "Http404"; //routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http404"; break; case 404: routeData.Values["action"] = "Http404"; break; } } // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; IController errorsController = new ErrorsController(); HttpContextWrapper wrapper = new HttpContextWrapper(Context); var rc = new RequestContext(wrapper, routeData); errorsController.Execute(rc); }
public void HandleError(Exception exception) { _httpContext.Response.Clear(); _httpContext.Server.ClearError(); if (!IfAjaxRequest()) { RouteData routeData = new RouteData(); routeData.Values["controller"] = "Errors"; _httpContext.Response.Clear(); routeData.Values["action"] = "Error"; var httpException = exception as HttpException; if (httpException != null && httpException.GetHttpCode() == (int)HttpStatusCode.NotFound) { routeData.Values["result"] = "404"; } else { routeData.Values["result"] = "500"; } IController errorController = new ErrorsController(); errorController.Execute(new RequestContext(new HttpContextWrapper(_httpContext), routeData)); } else { _httpContext.Response.Write("error"); _httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; _httpContext.Response.End(); } }
protected void Application_Error(object sender, EventArgs e) { if (!HttpContext.Current.IsCustomErrorEnabled) { return; } var httpContext = ((MvcApplication)sender).Context; var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); var currentController = " "; var currentAction = " "; if (currentRouteData != null) { if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString())) { currentController = currentRouteData.Values["controller"].ToString(); } if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString())) { currentAction = currentRouteData.Values["action"].ToString(); } } var ex = Server.GetLastError(); var controller = new ErrorsController(); var routeData = new RouteData(); var action = "Index"; if (ex is HttpException) { var httpEx = ex as HttpException; switch (httpEx.GetHttpCode()) { case 404: action = "Error404"; break; // others if any default: action = "Index"; break; } } httpContext.ClearError(); httpContext.Response.Clear(); httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500; httpContext.Response.TrySkipIisCustomErrors = true; routeData.Values["controller"] = "Errors"; routeData.Values["action"] = action; controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction); ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); }
public void Error404_Returns_View() { var controller = new ErrorsController(); var result = controller.Error404(); Assert.IsType <ViewResult>(result); }
public void AccessDenied_Returns_View() { var controller = new ErrorsController(); var result = controller.AccessDenied(); Assert.IsType <ViewResult>(result); }
public void Http403Test() { ErrorsController controller = new ErrorsController(); ViewResult result = controller.Http403() as ViewResult; Assert.IsNotNull(result); Assert.IsTrue(result.ViewName.Equals("403")); }
public void Test_Errors_NotFound_should_return_ViewResult_with_404_view() { var controller = new ErrorsController(); var result = controller.NotFound(); Assert.IsType <ViewResult>(result); var viewResult = result as ViewResult; Assert.Equal("404", viewResult.ViewName); }
// From http://stackoverflow.com/a/9026907 protected void Application_EndRequest() { if (Context.Response.StatusCode == 400) { Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = ""; rd.Values["controller"] = "Errors"; rd.Values["action"] = "BadRequest"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } else if (Context.Response.StatusCode == 403) { Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = ""; rd.Values["controller"] = "Errors"; rd.Values["action"] = "NotAuthorized"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } else if (Context.Response.StatusCode == 404) { Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = ""; rd.Values["controller"] = "Errors"; rd.Values["action"] = "NotFound"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } else if (Context.Response.StatusCode == 500) { if (!Debugger.IsAttached) { Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = ""; rd.Values["controller"] = "Errors"; rd.Values["action"] = "InternalServerError"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } } }
void Application_Error(object sender, EventArgs e) { //try-catch added to aviod process on process unnecessary exception try { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; //Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); Response.TrySkipIisCustomErrors = true; switch (Response.StatusCode) { case 500: { routeData.Values["action"] = "Http500"; routeData.Values["exception"] = exception; break; } case 403: { routeData.Values["action"] = "Http403"; routeData.Values["exception"] = exception; break; } case 404: { routeData.Values["action"] = "Http404"; routeData.Values["exception"] = exception; break; } } } IController errorsController = new ErrorsController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); } catch (Exception) { // throw; } }
protected void Application_Error(object sender, EventArgs e) { Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = "AreaName"; // In case controller is in another area rd.Values["controller"] = "Errors"; rd.Values["action"] = "ExceptionError"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); }
protected void Application_EndRequest() { if (Context.Response.StatusCode == 404) { Response.Clear(); var rd = new RouteData(); rd.Values["controller"] = "Errors"; rd.Values["action"] = "NotFound404"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } }
public void Execute() { if (_context.Response.StatusCode == 404) { _response.Clear(); var rd = new RouteData(); rd.Values["controller"] = "Errors"; rd.Values["action"] = "NotFound"; IController c = new ErrorsController(); c.Execute(new RequestContext(_context, rd)); } }
protected void Application_Error(object sender, EventArgs e) { // 發生未處理錯誤時執行的程式碼 var app = (MvcApplication)sender; var ex = app.Server.GetLastError(); var context = app.Context; context.Response.Clear(); context.ClearError(); var httpException = ex as HttpException; if (httpException == null) { httpException = new HttpException(null, ex); } var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "Index"; routeData.Values["exception"] = ex; routeData.Values["from_Application_Error_Event"] = true; if (httpException != null) { switch (httpException.GetHttpCode()) { case 404: routeData.Values["action"] = "PageNotFound"; break; default: routeData.Values["action"] = "Index"; break; } } // Pass exception details to the target error View. routeData.Values.Add("error", ex.Message); // Avoid IIS7 getting in the middle context.Response.TrySkipIisCustomErrors = true; IController controller = new ErrorsController(); controller.Execute(new RequestContext(new HttpContextWrapper(context), routeData)); }
private void MoveToError(string action, HandleErrorInfo model) { var rd = new RouteData(); rd.Values["controller"] = "Errors"; rd.Values["action"] = action; var c = new ErrorsController(); if (model != null) { c.ViewData.Model = model; } ((IController)c).Execute(new RequestContext(new HttpContextWrapper(Context), rd)); }
protected void Application_Error(object sender, EventArgs e) { Exception lastException = Server.GetLastError(); //log by reading lastException.Message string actionName = Request.Path; Error error = new Error(); ErrorsController ec = new ErrorsController(); error.errorValue = ec.LogError(User.Identity.Name, lastException, "", actionName); db.Errors.Add(error); db.SaveChanges(); Server.ClearError(); Response.Redirect("\\Error.html"); }
protected void Application_EndRequest() { if (Context.Response.StatusCode == 404) { Response.Clear(); var routedata = new RouteData(); routedata.DataTokens["area"] = "ErrorArea"; // In case controller is in another area routedata.Values["controller"] = "Errors"; routedata.Values["action"] = "NotFound"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), routedata)); } }
protected void Application_EndRequest() { if (Context.Response.StatusCode == 404) { Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = "AreaName"; // In case controller is in another area rd.Values["controller"] = "Welcome"; rd.Values["action"] = "LoginUser"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); } }
/// <summary> /// Handles the Error event of the Application control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 400: routeData.Values["action"] = "Http400"; break; case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; case 500: routeData.Values["action"] = "Http500"; break; } } // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; IController errorsController = new ErrorsController(); var wrapper = new HttpContextWrapper(Context); var rc = new RequestContext(wrapper, routeData); // Log to file (Debug Level) when http statuscode 500 (Internal Server Error) if (Response.StatusCode == 500) { DyntaxaLogger.WriteDebugException(exception); } errorsController.Execute(rc); }
protected void Application_Error(object sender, EventArgs eventArgs) { Exception exception = Server.GetLastError(); Server.ClearError(); Response.TrySkipIisCustomErrors = true; Response.Clear(); ErrorsController errorsController = new ErrorsController(); errorsController.ViewBag.Exception = exception; RouteData routeData = new RouteData(); routeData.Values["Culture"] = HttpContext.Current.Request.RequestContext.RouteData.Values["culture"]; routeData.Values["Controller"] = "Errors"; routeData.Values["Action"] = "Error500"; (errorsController as IController).Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); }
protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); if (ex != null) { StringBuilder err = new StringBuilder(); err.Append("Error caught in Application_Error event\n"); err.Append("Error in: " + (Context.Session == null ? string.Empty : Request.Url.ToString())); err.Append("\nError Message:" + ex.Message); if (null != ex.InnerException) { err.Append("\nInner Error Message:" + ex.InnerException.Message); } err.Append("\n\nStack Trace:" + ex.StackTrace); Server.ClearError(); if (null != Context.Session) { err.Append($"Session: Identity name:[{Thread.CurrentPrincipal.Identity.Name}] IsAuthenticated:{Thread.CurrentPrincipal.Identity.IsAuthenticated}"); } _log.Error(err.ToString()); if (null != Context.Session) { var routeData = new RouteData(); routeData.Values.Add("controller", "Errors"); routeData.Values.Add("action", "Error"); routeData.Values.Add("exception", ex); if (ex.GetType() == typeof(HttpException)) { routeData.Values.Add("statusCode", ((HttpException)ex).GetHttpCode()); } else { routeData.Values.Add("statusCode", 500); } Response.TrySkipIisCustomErrors = true; IController controller = new ErrorsController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); Response.End(); } } }
public void Other() { // Arrange var context = new Mock <ControllerContext>(); context.Setup(x => x.HttpContext.Response.StatusCode).Verifiable(); using (var controller = new ErrorsController()) { controller.ControllerContext = context.Object; // Act var result = controller.Other(null) as ViewResult; // Assert Assert.IsNotNull(result); context.VerifySet(x => x.HttpContext.Response.StatusCode = 500); } }
protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 400: routeData.Values["action"] = "Http400"; break; case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } Session["ErrorException"] = exception; IController errorsController = new ErrorsController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); }
protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; Response.ContentType = "text/html"; // needed to render views rather than text response if (httpException != null) { // referring url var url = HttpContext.Current.Request.Url; Response.StatusCode = httpException.GetHttpCode(); System.Diagnostics.Debug.WriteLine("Status Code: " + Response.StatusCode + " for " + url); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; case 500: routeData.Values["action"] = "Http500"; break; } } IController errorsController = new ErrorsController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); }
private void ShowCustomErrorPage(Exception exception) { var httpException = exception as HttpException ?? new HttpException(500, "Internal Server Error", exception); Response.Clear(); var routeData = new RouteData(); routeData.Values.Add("controller", "Errors"); routeData.Values.Add("fromAppErrorEvent", true); switch (httpException.GetHttpCode()) { //Remove incase of securtity case 403: routeData.Values.Add("action", "HttpError403"); //routeData.Values.Add("error", httpException.Message); break; case 404: routeData.Values.Add("action", "HttpError404"); //routeData.Values.Add("error", httpException.Message); break; case 500: routeData.Values.Add("action", "HttpError500"); //routeData.Values.Add("error", httpException.Message); break; default: routeData.Values.Add("action", "GeneralError"); //routeData.Values.Add("httpStatusCode", httpException.GetHttpCode()); break; } Server.ClearError(); IController controller = new ErrorsController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); }
protected void Application_EndRequest(object sender, EventArgs e) { if (Context.Response.StatusCode != (int)HttpStatusCode.NotFound) { return; } // Handle 404s by executing another controller action rather than turning on customErrors so developers still see // the "Yellow Screen of Death" for errors other that 404s, but we still get the same "Not Implemented" custom 404 // page as the Java training code Response.Clear(); var rd = new RouteData(); rd.DataTokens["area"] = string.Empty; rd.Values["controller"] = "Errors"; rd.Values["action"] = "NotFound"; IController c = new ErrorsController(); c.Execute(new RequestContext(new HttpContextWrapper(Context), rd)); }
protected void Application_Error() { if (!HttpContext.Current.IsCustomErrorEnabled) { return; } var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } Response.TrySkipIisCustomErrors = true; IController errorsController = new ErrorsController(); var wrapper = new HttpContextWrapper(Context); var requestContext = new RequestContext(wrapper, routeData); errorsController.Execute(requestContext); }
public void OnStartup() { Boss boss = ObjectModel.Create("Application"); var handler = boss.Get<IMenuHandler>(); handler.Register(this, 53, () => {--m_current; DoHandle();}, this.DoCanGotoPreviousError); handler.Register(this, 54, () => {++m_current; DoHandle();}, this.DoCanGotoNextError); m_controller = new ErrorsController(); Unused.Value = m_controller.Retain(); }
void MvcApplication_Error(object sender, System.EventArgs e) { var exception = Server.GetLastError(); var httpException = exception as HttpException; bool success = RaiseErrorSignal(exception); Response.Clear(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "InternalServerError"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = eAction.Forbidden; break; case 404: routeData.Values["action"] = eAction.NotFound; break; case 401: routeData.Values["action"] = eAction.Unauthorized; break; } } Response.TrySkipIisCustomErrors = true; Server.ClearError(); var errorsController = new ErrorsController() as IController; var wrapper = new HttpContextWrapper(Context); wrapper.Response.ContentEncoding = Encoding.UTF8; var requestContext = new RequestContext(wrapper, routeData); errorsController.Execute(requestContext); }