Ok, I found a solution thanks to @ alistair-findlay and this website . Here's what web.config looks like now:
<system.web> <customErrors mode="On" defaultRedirect="~/Error/General" redirectMode="ResponseRewrite"> </customErrors> </system.web> <system.webServer> <httpErrors errorMode="Detailed" defaultResponseMode="Redirect"> <clear/> <error statusCode="404" path="/Error/HttpError404"/> </httpErrors> </system.webServer
And this is Global.asax.cs:
protected void Application_Error() { if (Context.IsCustomErrorEnabled) ShowCustomErrorPage(Server.GetLastError()); } 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", "Error"); routeData.Values.Add("fromAppErrorEvent", true); switch (httpException.GetHttpCode()) { case 403: routeData.Values.Add("action", "HttpError403"); break; case 404: routeData.Values.Add("action", "HttpError404"); break; case 500: routeData.Values.Add("action", "HttpError500"); break; default: routeData.Values.Add("action", "GeneralError"); routeData.Values.Add("httpStatusCode", httpException.GetHttpCode()); break; } Server.ClearError(); IController controller = new ErrorController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); }
And finally:
public class ErrorController : Controller { public ActionResult HttpError403(string error) { ViewBag.Description = error; return this.View(); }
mortenstarck
source share