{STATIC} hippo

...on becoming a great developer...
posts - 38, comments - 12, trackbacks - 0

My Links

Archives

Post Categories

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!

Print | posted on Sunday, November 08, 2009 9:20 PM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: MVC RenderAction or RenderPartial graceful exception handling

I think the try catch is probably a little too broad. From a programmatic and performance standpoint, it typically better to catch specific scenarios.

In your custom view engine class...

public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
var partial = base.FindPartialView(controllerContext, partialViewName, useCache);
return partial.View != null
? partial
: base.FindPartialView(controllerContext, "~/views/shared/partialNotFound.ascx", true);
}

The code above will map to a blank partial, in the event of one not being found, etc...
12/17/2009 11:05 AM | Jamison Morrow
Gravatar

# re: MVC RenderAction or RenderPartial graceful exception handling

The exceptions aren't in finding the View but rather Rendering it. That's why I used the try/catch. This also gives me the ability to log exceptions so we can see what's going wrong.
3/2/2010 10:59 PM | noah

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 3 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET