2017-07-13 4 views
0

私はこの投稿の解決策を使用して私のアプリケーションのエラー処理をまとめましたLink。私が得ている問題は、アプリケーションがまだデフォルトのエラーページにルーティングされているということです。このコードは、ブレークポイント時にGlobal.asaxに至りますが、ビューのカスタムエラーページを読み込むと、ソリューションからデフォルトのエラーページが読み込まれます。私はデフォルトのエラーページを削除しようとしましたが、その後、IISから黄色のエラーページが表示されました。ウェブをしっとりと探しましたが、結果はありませんでした。すべての助けに感謝します。私が質問やタイトルを微調整することができると思うなら、私は提案をすることができます。カスタムエラーハンドラは依然としてデフォルトのエラーページをロードします

エラーコントローラ:Global.asax.csで

public class ErrorController : Controller 
{ 
    public ActionResult PageNotFound(Exception ex) 
    { 
     Response.StatusCode = 404; 
     return View("Error", ex); 
    } 

    public ActionResult ServerError(Exception ex) 
    { 
     Response.StatusCode = 500; 
     return View("Error", ex); 
    } 

    public ActionResult UnauthorisedRequest(Exception ex) 
    { 
     Response.StatusCode = 403; 
     return View("Error", ex); 
    } 

    //Any other errors you want to specifically handle here. 

    public ActionResult CatchAllUrls() 
    { 
     //throwing an exception here pushes the error through the Application_Error method for centralised handling/logging 
     throw new HttpException(404, "The requested url " + Request.Url.ToString() + " was not found"); 
    } 
} 

コード:

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

     //Error logging omitted 

     HttpException httpException = exception as HttpException; 
     RouteData routeData = new RouteData(); 
     IController errorController = new Controllers.ErrorController(); 
     routeData.Values.Add("controller", "Error"); 
     routeData.Values.Add("area", ""); 
     routeData.Values.Add("ex", exception); 

     if (httpException != null) 
     { 
      //this is a basic example of how you can choose to handle your errors based on http status codes. 
      switch (httpException.GetHttpCode()) 
      { 
       case 404: 
        Response.Clear(); 

        // page not found 
        routeData.Values.Add("action", "PageNotFound"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 

        break; 
       case 500: 
        // server error 
        routeData.Values.Add("action", "ServerError"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
        break; 
       case 403: 
        // server error 
        routeData.Values.Add("action", "UnauthorisedRequest"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
        break; 
       //add cases for other http errors you want to handle, otherwise HTTP500 will be returned as the default. 
       default: 
        // server error 
        routeData.Values.Add("action", "ServerError"); 

        Server.ClearError(); 
        // Call the controller with the route 
        errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
        break; 
      } 
     } 
     //All other exceptions should result in a 500 error as they are issues with unhandled exceptions in the code 
     else 
     { 
      routeData.Values.Add("action", "ServerError"); 
      Server.ClearError(); 
      // Call the controller with the route 
      errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
     } 
    } 

RouteConfig.cs

routes.MapRoute("CatchAllUrls", "{*url}", new {controller = "Error", action = "CatchAllUrls"}); 

catchブロック:

throw new HttpException(404, "Unable to write to list" + ", " + e.InnerException); 
+0

web.configの ''セクションに ''がありますか? –

+0

これはあなたのErrorController getが実行されていますか?代わりにリダイレクトする必要があると思います。 [この記事](https://www.codeproject.com/Articles/850062/Exception-handling-in-ASP-NET-MVC-methods-explaine)を参照してください。 – miszczak

+0

6人のexaplesのうちの1人が私に最も適していると思いますか? – AllramEst

答えて

0

@GeorgPatscheiderと@AliAshrafの助けを借りて、私は最終的に私の問題を解決しました。私は、この戻り値を変更することによって動作するようにしました。View( "Error"、ex.Message);この戻り値にView( "Error"、ex);そして<customErrors mode="On"/>を加えた。 HTTPエラーコードを追加することもできます。最初の投稿の後にMessageexに追加しました。ありがとうございました!

関連する問題