I faced the same issue but now I found a solution. Maybe you can try this: Just rename the class names or variable names to your needs. Take note after changing anything from your Global.asax clear your browser cache. Hope this helps.
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Make sure this route is the first one to be added
routes.MapRoute(
"ErrorHandler",
"ErrorHandler/{action}/{errMsg}",
new { controller = "ErrorHandler", action = "Index", errMsg=UrlParameter.Optional}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Once an unhandles exception occurs redirect the response to your error handler from your Global.asax Application_Error event
protected void Application_Error(object sender, EventArgs e)
{
var errMsg = Server.GetLastError().Message;
if (string.IsNullOrWhiteSpace(errMsg)) return;
//Make sure parameter names to be passed is are not equal
Response.RedirectToRoute("ErrorHandler", new { strErrMsg=errMsg });
this.Context.ClearError();
}
Error Handler Controller
public class ErrorHandlerController : Controller
{
public ActionResult Index(string strErrMsg)
{
ViewBag.Exception = strErrMsg;
return View();
}
}
To test the error handler on your Index ActionResult of HomeController add this code.
public class HomeController : Controller
{
public ActionResult Index()
{
//just intentionally add this code so that exception will occur
int.Parse("test");
return View();
}
}
The output will be