Argh! Why does System.Web.Mvc.HandleErrorInfo get passed to my views?

Here is an issue on codeplex explaining why that error occurs.

Quote from http://web.archive.org/web/20131004122626/http://aspnet.codeplex.com/workitem/1795 since original link is dead:

HandleError Attribute should not store exception information in ViewData

When the HandleError attribute handles an exception, it stores the exception information in the ViewData. This is a problem when the Error.aspx inherits from the site.master and the site.master class is declared as follows.

public partial class Site : System.Web.Mvc.ViewMasterPage<SiteViewData>
{
}

SiteViewData contains:

public class SiteViewData 
{
  public String Title { get; set; } 
}

Each page ViewData class inherits from the SiteViewData class and looks something like this

public class IndexViewData : SiteViewData
{
  public String Message { get; set; }
  public String SupportedLanguages {get; set;}
}

This approach allows one to write code in the Site.Master page as follows

<title><%= Html.Encode(ViewData.Model.Title) %></title>

Unfortunately, when an exception is thrown, the model has been replaced with an instance of the HandleErrorInfo class. This causes an InvalidOperationException to be thrown with the information

The model item passed into the dictionary is of type System.Web.Mvc.HandleErrorInfo but this dictionary requires a model item of type Igwt.Boh.Website.Web.Controllers.SiteViewData.

Is it possible for a new ErrorData property to be added to the ViewResult class to store the instance of the HandleErrorInfo class instead? This way the ViewData does not get changed.

Chances are pretty good that any exception thrown in the action will occur after the IndexViewData (and SiteViewData) properties have already been initialized.

Closed Jan 27, 2010 at 12:24 AM by

Won’t fix – see comments.


The comments mentioned with “wontfix” are from a former member of the Microsoft team, along with their suggestion for working around it (bolded):

By the time the [HandleError] attribute executes, we’ve lost the
reference to the original ActionResult object. We don’t even know if
you intended to show a view anyway – maybe you intended to redirect.
The part of the pipeline (the ViewResult) that would have been
responsible for passing the model from the controller to the view is
gone.

If an exception occurs, any model the application was working on
should probably be treated as corrupt or unavailable anyway. The best
practice would be to write your Error view such that neither it nor
its dependencies (such as its master page) requires the original
model.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)