Correct Exception Handling with ASP.NET MVC, ELMAH and Custom Error Pages

consider the following situation:

There is an ASP.NET MVC application that uses ELMAH for centralized ExceptionLogging. The controller is marked with the HandlerError attribute to catch the specific type of exception and present the view to the user. for example

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

      

This works as expected. The problem I am facing now is that the "ModelSpecific" error page needs some objects in the ViewData. Does anyone have a hint of populating a ViewData ViewPage dictionary of the following type

System.Web.Mvc.ViewPage<HandleErrorInfo>

      

Another idea that comes to my mind is that maybe Controller can be used to ErrorHandling with appropriate ActionResults. But I currently don't know how to do this.

Any help is greatly appreciated ...

Regards,

Gordon

+2


source to share


1 answer


Since your exception class and view are model specific, can you store the additional data needed in the exception itself?

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

      



In your view, you can get the exception through Server.GetLastError () and then cast on the correct type to access additional data through properties. This might be a cleaner approach as it treats the exception as a model and prevents you from displaying the ViewData collection.

0


source







All Articles