在我的Global.asax中,我定义了Application_error方法:
protected void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
// Get the exception object.
var exc = Server.GetLastError();
//logics
Response.Redirect(String.Format("~/ControllerName/MethodName?errorType={0}", errorAsInteger));
}
exc 变量确实保留了最后一个错误,但在从响应方法(MethodName)中的响应重定向后,Server.GetLastError()
为 null。如何保留它或将其传递给Response.Redirect(String.Format("~/ControllerName/MethodName?errorType={0}"
以便我可以将异常作为对象在方法主体中?
我想建议您在出现错误时不要重定向,以便保留 URL 并设置正确的 HTTP 状态代码。
相反,请在 Application_Error
中执行控制器
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
var httpContext = ((HttpApplication)sender).Context;
httpContext.Response.Clear();
httpContext.ClearError();
ExecuteErrorController(httpContext, exception);
}
private void ExecuteErrorController(HttpContext httpContext, Exception exception)
{
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "Index";
routeData.Values["errorType"] = 10; //this is your error code. Can this be retrieved from your error controller instead?
routeData.Values["exception"] = exception;
using (Controller controller = new ErrorController())
{
((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
}
}
那么错误控制器是
public class ErrorController : Controller
{
public ActionResult Index(Exception exception, int errorType)
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = GetStatusCode(exception);
return View();
}
private int GetStatusCode(Exception exception)
{
var httpException = exception as HttpException;
return httpException != null ? httpException.GetHttpCode() : (int)HttpStatusCode.InternalServerError;
}
}
TempData 的值将一直持续到读取或会话超时。 以这种方式保留 TempData 可以实现重定向等方案,因为 TempData 中的值在单个请求之外可用。
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());
然后在您的操作中,您可以使用
var error = TempData["LastError"];
但是这是您可以在不重定向的情况下完成的其他解决方案
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));
}
然后在控制器中您可以添加操作
public ActionResult HttpError500(Exception error)
{
return View();
}