2013-06-02 141 views
4

在Global.asax中,我們有一個名爲MvcApplication 的類System.Web.HttpApplication類,它表示應用程序並且我們可以在其中處理各種事件。Global.asax中的ASP.NET MVC Application_Error處理程序

我對Application_Error處理程序感興趣。 在這個處理程序中,我們可以使用類MvcApplication的所有屬性。

-1-

始終爲真該 '(MvcApplication)發件人' 和 '本' 是同一個對象?

protected void Application_Error(object sender, EventArgs e) 
{ 
    var httpApp = (MvcApplication)sender; 
    var equality1 = httpApp == this; // always true? 
} 

-2-

什麼是得到錯誤的最好方法? 以下示例返回相同的錯誤?

Exception ex0 = this.Context.Error; 
Exception ex1 = httpContext.Error; 
Exception ex2 = Server.GetLastError(); 
var equality3 = ex1 == ex2; // true? 

回答

2
protected void Application_Error(object sender, EventArgs e) { 
    Exception exception = Server.GetLastError(); 
    Response.Clear(); 

    HttpException httpException = exception as HttpException; 

    if (httpException != null) { 
    string action; 

    switch (httpException.GetHttpCode()) { 
     case 404: 
     // page not found 
     action = "HttpError404"; 
     break; 
     case 500: 
     // server error 
     action = "HttpError500"; 
     break; 
     default: 
     action = "General"; 
     break; 
     } 

     // clear error on server 
     Server.ClearError(); 

     Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message)); 
    } 

而在你的控制器:

// GET: /Error/HttpError404 
public ActionResult HttpError404(string message) { 
    return View("SomeView", message); 
} 

ASP.NET MVC Custom Error Handling Application_Error Global.asax?

3

的Global.asax

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception exception = Server.GetLastError(); 
     Response.Clear(); 

     if (exception != null) 
     { 
      Common.WriteErrorLog(exception); 
     } 

     HttpException httpException = exception as HttpException; 
     if (httpException != null) 
     { 
      RouteData routeData = new RouteData(); 
      routeData.Values.Add("controller", "Error"); 
      switch (httpException.GetHttpCode()) 
      { 
       case 404: 
        // page not found 
        routeData.Values.Add("action", "HttpError404"); 
        break; 
       case 500: 
        // server error 
        routeData.Values.Add("action", "HttpError500"); 
        break; 
       default: 
        routeData.Values.Add("action", "Index"); 
        break; 
      } 
      routeData.Values.Add("error", exception.Message); 
      // clear error on server 
      Server.ClearError(); 

      Response.RedirectToRoute(routeData.Values); 
      // at this point how to properly pass route data to error controller? 
      //Response.Redirect(String.Format("~/Error/{0}/?message={1}", "Index", exception.Message)); 
     } 
    } 

控制器:

public class ErrorController : Controller 
{ 
    // GET: Error 
    public ActionResult Index(string error="") 
    { 
     ViewBag.Message = error; 
     return View(); 
    } 

    public ActionResult HttpError404(string error = "") 
    { 
     ViewBag.Message = error; 
     return View(); 
    } 

    public ActionResult HttpError500(string error = "") 
    { 
     ViewBag.Message = error; 
     return View(); 
    } 
} 
相關問題