As we might know, from e.g. What is the difference between customErrors and httpErrors?, CustomErrors is a older way to define error pages in a web application but this appr
Just like you, I think ASP.NET shouldn't set TrySkipIisCustomErrors or may add an option so we can avoid it.
As a workaround, I built an ASPX page that is able to transfer the query to an ASP.NET MVC Controller.
ErrorHandler.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ErrorHandler.aspx.cs" Inherits="Guillaume.ErrorHandler" %>
Code behind
protected void Page_Load(object sender, EventArgs e)
{
//Get status code
var queryStatusCode = Request.QueryString.Get("code");
int statusCode;
if (!int.TryParse(queryStatusCode, out statusCode))
{
var lastError = Server.GetLastError();
HttpException ex = lastError as HttpException;
statusCode = ex == null ? 500 : ex.GetHttpCode();
}
Response.StatusCode = statusCode;
// Execute a route
RouteData routeData = new RouteData();
string controllerName = Request.QueryString.Get("controller") ?? "Errors";
routeData.Values.Add("controller", controllerName);
routeData.Values.Add("action", Request.QueryString.Get("action") ?? "Index");
var requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
IController controller = ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, controllerName);
controller.Execute(requestContext);
}
Usage in web.config
The behavior is correct on regular browser request : the error page is shown when there is an error and the error code is returned. It's also correct on an AJAX/Web API request : the error page is NOT returned. We get a parsable error in JSON or XML.
You may add a httpErrors section if you want non-ASP.NET errors to be redirected to your custom errors.