Chris Currie
Chris Currie

Reputation: 55

Why is 404 Custom Error working but 500 Custom Error not?

I have implemented CustomErrors in my web.config. The 404 error works as intended, the 500 errors simply show an empty default layout page with the standard generated .NET HTML output in the RenderBody area.

The code in the web.config is:

<customErrors mode="On">
  <error statusCode="404" redirect="~/404.html"/>
  <error statusCode="500" redirect="~/500.html"/>
</customErrors> 

404 can be seen here: http://www.airportcars-gatwick.com/i-do-not-exist

500 can be seen here: https://taxis.gatwickairport.com/Book?Q=9fa5-514e-4c3a-972f8baee58fa2b9

(Both files are in place on the server at the same root location)

This is the layout page with HTML injected into the RenderBody

</div>
 </header>
 <!-- //Header -->

 <!DOCTYPE html>
 <html>
  <head>
   <meta name="viewport" content="width=device-width" />
   <title>Error</title>
  </head>
  <body>
   <hgroup>
    <h1>Error.</h1>
    <h2>An error occurred while processing your request.</h2>
   </hgroup>
  </body>
 </html>

<!-- Footer -->
<footer class="footer teal" role="contentinfo">
    <div class="wrap">
 ..etc...

Not sure what I'm missing?

Upvotes: 1

Views: 681

Answers (1)

Chris Currie
Chris Currie

Reputation: 55

It appears the issue was with a default configuration of my GlobalFilters.

GlobalFilters is a way to associate an attribute with every Action method in the ASP.NET MVC application. I guess my project had automatically taken care of that for me.

Within my app_start folder within the FilterConfig.cs file:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
}

So by commenting out the RegisterGlobalFilters line in the Global.asax.cs file, the issue was resolved.

protected void Application_Start()
    {
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); // Comment out

Hope this helps if anyone else experiences the same behaviour.

Upvotes: 2

Related Questions