Asbestos Supply

2009-11-09 MVC RenderAction or RenderPartial graceful exception handling

We recently had a little dilemma that I'm sure others have had.  We have a lot of areas of our pages that are rendered using RenderPartial or RenderAction (part of MVC Futures) and we want graceful handling of exceptions.  Of course exceptions shouldn't happen, but sometimes they do (if only everyone practiced TDD…), and with 1M hits a day we would like to be able to at least show as much content (and ads ;) as we can – even if something does go wrong.

ViewEngine & IView

So I was approached about this issue and my solution was very simple:

We created a custom ViewEngine that returns a custom IView (just inherits from the default WebFormView) that wraps its base.Render() call in a try/catch. 

Very simple and works like a charm!