RegisterGlobalFilters and HandleErrorAttribute not working

5k views Asked by At

After struggling with RegisterGlobalFilters and HandleErrorAttribute forever I decided to go back to the bare bones. I created a new MVC 3 project in VS10, using the template. Add a throw DivideByZeroException in the About-action and fired up the dev server. Expected not to see a yellow screen.

But I did.

Why won't this work for me?

Update

archil's and Adam Tuliper's suggestion kind of worked. The Error view was called.

Then I proceed to add this in RegisterGlobalFilters.

filters.Add(new HandleErrorAttribute { ExceptionType = typeof(DivideByZeroException), View = "DivideByZeroException", Order = 1 });
filters.Add(new HandleErrorAttribute { View = "AllOtherExceptions", Order = 2 });

The AllOtherExceptions view was called. Why not the DivideByZeroException view?

The followup question has been posted here.

2

There are 2 answers

1
archil On BEST ANSWER

HandleErrorAttribute works when following conditions are met

  • CustomErrors is enabled in web config
  • If exception is HttpException, its error code is 500.

As in your case second condition is met, ensure you have custom erros turned on

<system.web>
     <customErrors mode="On"></customErrors>
</system.web>
0
Adam Tuliper On

Do you have custom errors turned ON in your web.config? If not turn it on, and it should work.