如何在响应重定向MVC后保留Server.GetLastError

时间:2014-01-14 12:33:56

标签: asp.net-mvc model-view-controller response global-asax application-error

在我的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()为空。如何保留它或将其传递给Response.Redirect(String.Format("~/ControllerName/MethodName?errorType={0}",以便我可以将我的方法体中的异常作为对象?

2 个答案:

答案 0 :(得分:13)

如果出现错误,我建议您重定向,以便保留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));
    }
}

然后ErrorController是

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;
    }
}

答案 1 :(得分:2)

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));
        }

然后在Controller中你可以添加动作

public ActionResult HttpError500(Exception error)
        {
            return View();
        }