3

以下のコードを使用して例外処理を実装しました。[編集開始]ビューを2回呼び出す理由がわかりません。[編集完了]

ベースコントローラー

public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;

                if (filterContext.Exception.GetType() == typeof(ArgumentOutOfRangeException))
                {
                    this.View("OutOfRange").ExecuteResult(this.ControllerContext);
                }
                else
                {
                    this.View("Error").ExecuteResult(this.ControllerContext);
                }
            }

            base.OnException(filterContext);
        }
    }

ホームコントローラー

public class HomeController : BaseController
{
        public ActionResult Exception2()
        {
            throw (new ArgumentOutOfRangeException());
        }

        public ActionResult Exception3()
        {
            throw (new Exception());
        }
}

エラー表示(共有フォルダのみ)

@model System.Web.Mvc.HandleErrorInfo
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Error</title>
</head>
<body>
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
    <h3>
        @if (Model != null)
        {
            <p>@Model.Exception.GetType().Name<br />
                thrown in @Model.ControllerName @Model.ActionName</p>
            <br />
            @Model.Exception
        }
    </h3>
</body>
</html>

OutOfRange ビュー (共有フォルダーのみ)

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>OutOfRange Exception</title>
</head>
<body>
    <div>
        <h3>
            @if (Model != null)
            {
                <p>@Model.Exception.GetType().Name<br />
                    thrown in @Model.ControllerName @Model.ActionName</p>
                <br />
                @Model.Exception
            }
        </h3>
    </div>
</body>
</html>

実行 URL :http://[domainName]/Home/Exception2

ここでは、正常に動作しています。[編集: ここでも 2 回呼び出します。]

ここに画像の説明を入力

実行 URL :http://[domainName]/Home/Exception3

ここでは、うまく機能していません。「申し訳ありませんが、リクエストの処理中にエラーが発生しました」と表示されます。2回来ています。上記の URL を使用してアプリケーションをデバッグすると、エラー ビューが 2 回呼び出されました (最初はモデルが null で、2 回目はモデルに何らかの値が含まれています)。実装の何が問題なのかを知ることができますか? ここに画像の説明を入力

4

3 に答える 3

7

試すこと:

  1. デフォルトのHandleErrorグローバル アクション フィルタ属性Global.asaxが存在する場合は、それを削除します。新しい ASP.NET MVC 3 アプリケーションを作成すると、既定のテンプレートによってRegisterGlobalFiltersメソッド内に登録されます。したがって、この属性を登録する行をコメントアウトします。
  2. web.config でカスタム エラーを有効にします。

    <customErrors mode="On" />
    

アップデート:

モデルをビューに渡したい場合Error.cshtml(強く型付けされているためHandleErrorInfo)、次のようにすることができます。

else
{
    string controllerName = filterContext.RouteData.GetRequiredString("controller");
    string actionName = filterContext.RouteData.GetRequiredString("action");
    var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
    var result = new ViewResult
    {
        ViewName = "Error",
        ViewData = new ViewDataDictionary<HandleErrorInfo>(model)
    };
    filterContext.Result = result;
}
于 2012-07-04T06:06:51.193 に答える
1

base.OnException(filterContext);前に 追加if (filterContext.HttpContext.IsCustomErrorEnabled)

      protected override void OnException(ExceptionContext filterContext)
      {
          base.OnException(filterContext);
         if (filterContext.HttpContext.IsCustomErrorEnabled)

      }
于 2012-07-03T14:11:26.600 に答える