views:

935

answers:

1

Hi folks,

consider the following situation:

There is an ASP.NET MVC application which utilizes ELMAH for centralized ExceptionLogging. A Controller is marked with the HandlerError Attribute to catch a specific type of an exception and presents the user with a view. For example

[HandleError(ExceptionType = typeof(ModelSpecificException), View = "Exceptions/ModelSpecific")]
public partial class HeavyController : Controller
{
  // Constructors and ActionResults are following here...
}

This is working as expected so far. The problem I am facing right now is, that the "ModelSpecific" error page is needing some Objects within the ViewData. Does anyone has a hint on populating the ViewData Dictionary of a ViewPage of following Type

System.Web.Mvc.ViewPage<HandleErrorInfo>

Another idea which comes to my my mind is, that maybe a Controller could be used for the ErrorHandling with respective ActionResults. But currently I don't have a clue on how to accomplish that.

Any help is very appreciated...

best regards,

Gordon

A: 

Since both your exception class and the view are model specific, could you store the extra data you need in the exception itself?

if(badCondition)
{
    throw new ModelSpecificException("a bad thing happened", extraData);
}

In your view you can get the exception via Server.GetLastError() and then cast it to the correct type to access the extra data via properties. This might be a cleaner approach since it treats the exception as the model and keeps you out of the ViewData collection.

Matthew Daugherty