Application_Error中的异常处理,sender.Context.Request是什么?

时间:2016-08-04 08:50:53

标签: asp.net asp.net-mvc asp.net-mvc-4

我的Application_Error中有异常处理代码。我从stackoverflow获得了代码,它正在运行。

protected void Application_Error(Object sender, EventArgs e)
{

    string currentController = "";
    string currentAction = "";

    HttpContext httpContext = ((MvcApplication)sender).Context;
    HttpRequestWrapper httpRequest = new HttpRequestWrapper(httpContext.Request);


    RouteData currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
    if (!(currentRouteData == null))
    {
        if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
        {
            currentController = currentRouteData.Values["controller"].ToString();
        }
        if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
        {
            currentAction = currentRouteData.Values["action"].ToString();
        }
    }


    var exception = Server.GetLastError();
    if (exception == null)
        return;

    // Clear the error
    Server.ClearError();

    Models.Exception ex = new Models.Exception();
    ex.ErrorMessage = exception.Message;
    ex.ErrorUrl = String.Format("{0}/{1}", currentController, currentAction);

    FreelancerDB.SaveExceptions(ex);

    Response.Redirect("~/WebsiteAccess/SystemError");
}

问题在于我不明白这两个因素正在发生什么:

HttpContext httpContext = ((MvcApplication)sender).Context;
HttpRequestWrapper httpRequest = new HttpRequestWrapper(httpContext.Request);

有谁可以解释这里发生的事情?

1 个答案:

答案 0 :(得分:0)

那里什么都没发生,之后没有使用httpRequest变量。

它确实为您提供了生成异常的请求的引用,您可能需要它。