I have the following global error handling setup for a webapp.
Global.asax.cs
protected void Application_Error()
{
//log error
_logger.Error("something bad happened", Server.GetLastError());
//clear error
Server.ClearError();
//redirect to error page
Response.RedirectToRoute(new { controller = "services", action = "error" });
}
Services controller with Error Action. This view uses the ErrorModel class as the model.
public ActionResult Error()
{
return View(new ErrorModel{ Message = "nothing really happened go to bed" });
}
General view:
public ActionResult General()
{
throw new Exception("Exception XYZ");
}
When I debug the application using VS F5, and go to /general
view, the correct exception gets logged (Exception XYZ
).
But when testing the published IIS webapp, and going to /general
view, a different, unexpected exception is getting logged:
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type 'MyWebsite.Models.Services.ErrorModel'.
The Exception XYX
does not get logged at all. The correct Error
view with the correct error message (nothing really happened go to bed
) does get shown.
What am I doing wrong? How do I make sure Exception XYZ
gets logged?
I don't intend to use HandleErrorAttribute
or override void OnException
in the controller at this time.
EDIT Seems like this issue is already discussed in 2010 on SO here