The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.
Dictionary<string, object> tempDataDictionary = HttpContext.Current.Session["__ControllerTempData"] as Dictionary<string, object>;
if (tempDataDictionary == null)
{
tempDataDictionary = new Dictionary<string, object>();
HttpContext.Current.Session["__ControllerTempData"] = tempDataDictionary;
}
tempDataDictionary.Add("LastError", Server.GetLastError());
Then in your action you can use
var error = TempData["LastError"];
But here is other solution which you can do without redirect
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpException == null)
{
routeData.Values.Add("action", "HttpError500");
}
else
{
switch (httpException.GetHttpCode())
{
case 404:
routeData.Values.Add("action", "HttpError404");
break;
default:
routeData.Values.Add("action", "HttpError500");
break;
}
}
routeData.Values.Add("error", exception);
Server.ClearError();
IController errorController = DependencyResolver.Current.GetService<ErrorController>();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
then in Controller you can add action
public ActionResult HttpError500(Exception error)
{
return View();
}
httpContext.Response.ContentType = "text/html";
underrouteData.Values["exception"] = exception;
and that should fix it. – Geniality