you can do this by registering the Application_Error event in Global.asax and then redirect it to the error controller with the data in this way or you can perform any function that you want, depending on the error code
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"] = "oops"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "NoAccess"; break; case 404: routeData.Values["action"] = "NotFound"; break; } } IController errorsController = new ErrorController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); }
then create a controller with oops / NotAccess / NotFound actions like
public ActionResult oops(Exception exception) { //return Content("General failure", "text/plain"); // return View(); } public ActionResult NotFound() { // return Content("Not found", "text/plain"); // return View("oops"); } public ActionResult NoAccess() { //return Content("Forbidden", "text/plain"); //return View("oops"); }
And bring back your own view with relevant information
source share