4

在我的 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}"以便我可以在我的方法主体中将异常作为对象?

4

2 回答 2

16

我想建议您在出现错误时不要重定向,以便保留 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;
    }
}
于 2014-01-14T13:49:35.973 回答
3

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();
        }
于 2014-01-14T13:40:35.223 回答