Server.GetLastError is not returning the real exception in my global error handler

777

New Member
I followed this answer to implement a global error handler. However, it's not working as it should.To test the error handler, I on purposely created an action in \[code\]HomeController\[/code\] that'll fail:\[code\]public ActionResult Index(){ string s = null; var ss = s.Length; return View();}\[/code\]And then in \[code\]Global.asax.cs\[/code\], I have:\[code\]protected void Application_Error(){ var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); Response.StatusCode = 500; var routeData = http://stackoverflow.com/questions/15845455/new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } Response.TrySkipIisCustomErrors = true; IController errorsController = new ErrorsController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc);}\[/code\]And in my \[code\]ErrorsController\[/code\], I have:\[code\]public class ErrorsController : Controller{ public ActionResult General(Exception exception) { // log error return Content("Error", "text/plain"); } public ActionResult Http403(Exception exception) { return Content("Forbidden", "text/plain"); } public ActionResult Http404(Exception exception) { return Content("Page not found", "text/plain"); }}\[/code\]It all works fine when testing in Visual Studio 2012, but once deployed to IIS 7.5 on server, I get the following error when the exception is thrown at:\[code\]string s = null;var ss = s.Length; The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:~/Views/Home/Error.aspx~/Views/Home/Error.ascx~/Views/Shared/Error.aspx~/Views/Shared/Error.ascx~/Views/Home/Error.cshtml~/Views/Home/Error.vbhtml~/Views/Shared/Error.cshtml~/Views/Shared/Error.vbhtml\[/code\]The weird thing is that, when I debug remotely, I follow the execution to the \[code\]var ss = s.Length;\[/code\], and then it goes to \[code\]var exception = Server.GetLastError();\[/code\] and the \[code\]exception\[/code\] is above error. Seems like the last error just got replaced?! I should have an object is null exception. What's going on?
 
Back
Top