2013-10-22 42 views
0

我不得不重構我的整個應用程序,從VS Express數據庫到使用Visual Studio 2012. 我必須刪除所有的模塊,並重新添加它們。MVC4 httpException.GetHttpCode();導致NullReferenceException

現在,當我嘗試運行應用程序,它使翻倒的global.asax.cs

它不斷拋出的NullReferenceException - > INT exceptionType = httpException.GetHttpCode();

我的應用程序不能運行,就好像本地主機服務器甚至沒有加載我的網站

請幫助

protected void Application_Error() 
     { 

      if (Context.IsCustomErrorEnabled) 
       ShowCustomErrorPage(Server.GetLastError()); 

     } 
     private void ShowCustomErrorPage(Exception exception) 
     { 
      var httpException = exception as HttpException; 
      if (exception != null) 
      { 
       exception = httpException; 
      } 

      else 
      { 

       exception = new HttpException(500, "Internal Server Error", exception); 
      } 
      Response.Clear(); 
      Server.ClearError(); 
      var routeData = new RouteData(); 
      routeData.Values.Add("Controller", "Error"); 
      routeData.Values.Add("fromAppErrorEvent", true); 

      int exceptionType = httpException.GetHttpCode(); 

      switch (exceptionType) 
      { 


       case 403: 
        routeData.Values.Add("action", "HttpError403"); 
        break; 

       case 404: 
        routeData.Values.Add("action", "NotFound"); 
        break; 

       case 500: 
        routeData.Values.Add("action", "HttpError500"); 
        break; 

       default: 
        routeData.Values.Add("action", "GeneralError"); 
        routeData.Values.Add("httpStatusCode", httpException.GetHttpCode()); 
        break; 
      } 



      IController controller = new Login.Controllers.ErrorController(); 
      controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 

回答

2

在上面httpException邏輯可以null - 試着改變你的代碼像這樣:

HttpException httpException = exception as HttpException 
    ?? new HttpException(500, "Internal Server Error", exception); 

Response.Clear(); 
Server.ClearError(); 
var routeData = new RouteData(); 
routeData.Values.Add("Controller", "Error"); 
routeData.Values.Add("fromAppErrorEvent", true); 

int exceptionType = httpException.GetHttpCode(); 
相關問題