ASP.NET MVC Error Handling

Sunday, January 11, 2015


Good Enough

In my quest to finish off my application in time for DemoCamp, I decided to tackle the mundane task of implementing custom error pages. Out of the box, ASP.NET MVC (MVC) comes with a 500 page that you can customize as long as you add the customErrors section to the Web.config file, other than that, you'll get the standard page that IIS provides for 404 errors. It's not essential to my application, but a custom 404 page is more pleasant when the user experiences one.

Custom error pages are much simpler in WebForms because all you needed was that customErrors section and then you just had to add the error codes you wanted a custom error page for and the location of the page. Once you did that, you were done. Not so with MVC.

I searched for the MVC solution, because no tutorial ever mentioned custom error pages for MVC and when I did find something it was long and convoluted. There is absolutely no consensus online about how to do this and it seemed like no solutions were simple. Finally, I found this Stack Overflow answer to my problem. The answer resonated with me. Why should I have to do all this work for custom error pages. Just use IIS custom error pages with a simple Error controller. So I tried it out and it seems to work just fine. All the proper error response codes are being returned correctly and the user doesn't get redirected. I could take the time and go into technical detail and fully understand why exactly everyone was doing it the other way and I do have the gist of why they did it the other way, but at the moment, I just need it to work and I don't want to spend the time for a full technical write-up. Which leads me to my next point.

There are so many answers online and I sometimes think the correct ones are the ones that keep popping up in some shape or form, which was the case for the custom error pages, but I don't think it was. It might be the "correct" way to do it, or the more extensible or robust or whatever, but it definitely wasn't the best answer for my situation. Like I said before I just need it to be functional and to work with most cases, and it does exactly that as far as I'm concerned. Maybe I'll come across something later when my application is in full swing, but until then, this solves my problem and I don't need to write a bunch of custom code for something that's aesthetic like custom error pages. Don't get me wrong, I understand the importance of a deeper understanding of exactly what you're doing, but for this specific task, I know enough.


More Posts