Reputation: 8505
Here is my web.config custom errors configuration:
<system.web>
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/400.aspx">
<error statusCode="404" redirect="~/404.aspx" />
<error statusCode="500" redirect="~/500.aspx" />
<error statusCode="400" redirect="~/400.aspx" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="404.html" responseMode="File"/>
<remove statusCode="500"/>
<error statusCode="500" path="500.html" responseMode="File"/>
<remove statusCode="400"/>
<error statusCode="400" path="400.html" responseMode="File"/>
</httpErrors>
</system.webServer>
Works fine for 404. But this localhost:44303/< script >< /script > returns: Runtime Error.
As well throwing a new Exception from an action intentionally redirects to mvc error page, which I want to override with my 500.aspx
Upvotes: 4
Views: 2896
Reputation: 959
After some research into the source code for the HandleErrorAttribute
that is added as a global filter, there is code that tries to force application to use the MVC Error page.
So what I did was subclass the HandleErrorAttribute
and effectively reverse that line:
public class MyHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
filterContext.HttpContext.Response.TrySkipIisCustomErrors = false;
}
}
Then replace the code in the App_Start/FilterConfig.cs to use the new MyHandleErrorAttribute:
filters.Add(new MyHandleErrorAttribute());
This should prevent MVC from overriding your custom 500 page.
Upvotes: 3