2

コントローラーでスローする例外をキャッチするように設定された_error.cshtmlを使用してmvcアプリをセットアップしました。

また、いくつかのページに、エラーをチェックしてから別のことを行うajaxの投稿がいくつかあります。

サーバーでは、すべての例外にフィルターを設定し、それがajaxリクエストであるかどうかを確認して、クライアントで逆シリアル化できるものを返します。問題は、応答後のステータスコードを500に設定しないと、ajaxにこのエラーが表示されず、適切なメッセージを表示できないことです。ステータスを500に設定すると、サーバーで何かが発生したことを示すデフォルトのIISエラーメッセージが表示されます。

ajax結果のページでいくつかのエラーを処理したいのですが、一般的なエラー処理は維持します。これは、サイトごとにカスタム500メッセージを許可するIIS設定ですか?web.configカスタムエラーのオン|オフは、私の場合は何の違いもありません。

4

1 に答える 1

3

ajaxリクエストかどうかをチェックするすべての例外であなたが持っているフィルターは、あなた自身で作られたフィルターですか?

少し似たような問題があり、標準のIISエラーを回避するために、フラグTrySkipIisCustomErrorsがtrueに設定されていることを確認する必要がありました。このフラグは、HttpContextのResponseオブジェクトにあります。

これも標準のHandleErrorフィルターによって実行されます。OnExceptionメソッドの実装の最後の行に注意してください。

    public virtual void OnException(ExceptionContext filterContext) {
        if (filterContext == null) {
            throw new ArgumentNullException("filterContext");
        }
        if (filterContext.IsChildAction) {
            return;
        }

        // If custom errors are disabled, we need to let the normal ASP.NET exception handler
        // execute so that the user can see useful debugging information.
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) {
            return;
        }

        Exception exception = filterContext.Exception;

        // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
        // ignore it.
        if (new HttpException(null, exception).GetHttpCode() != 500) {
            return;
        }

        if (!ExceptionType.IsInstanceOfType(exception)) {
            return;
        }

        string controllerName = (string)filterContext.RouteData.Values["controller"];
        string actionName = (string)filterContext.RouteData.Values["action"];
        HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
        filterContext.Result = new ViewResult {
            ViewName = View,
            MasterName = Master,
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            TempData = filterContext.Controller.TempData
        };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;

        // Certain versions of IIS will sometimes use their own error page when
        // they detect a server error. Setting this property indicates that we
        // want it to try to render ASP.NET MVC's error page instead.
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
于 2012-11-20T19:59:18.490 に答える