错误处理 MVC 视图与 PartialView

Error handling MVC Views vs PartialView

我已经阅读了很多关于 MVC 中的错误处理的内容(特别是关于应用 HandleError 属性以及关于在 Global.asax 上使用 Application_Error)。我有兴趣优雅地处理以下类型的异常:

目前我的应用程序表现如下

  1. 控制器中抛出的所有异常均未处理。他们到达 Global.asax
  2. 的 Application_Error 方法
  3. 视图中的所有异常都未处理并到达Global.asax
  4. 的Application_Error方法
  5. 在 Application_Error 方法中,我记录异常,决定应用程序是 运行 本地还是远程。如果是这样,我会向用户显示黄色屏幕,或者对自定义错误页面执行 Response.Redirect。

此逻辑适用于呈现父视图或父视图本身的控制器内部抛出的错误。这种逻辑的缺点是,当在应该呈现 PartialView 的子操作中抛出错误时,整个页面变得不可用。这是因为黄色错误屏幕或自定义错误页面占据了整个页面,不允许用户查看网页的其他部分。

我想要do/know的是,如果可以的话:

  1. 在局部视图中显示黄色错误屏幕,但正确呈现页面的其余部分。
  2. 将用户重定向到允许页面其余部分保持可用的部分视图错误页面。

处理这个具体案例:

"when an error is thrown inside a Child Action which should render a PartialView the whole page becomes unusable."

您可以为 "ajaxError" 定义全局 Ajax 事件处理程序来处理使用“@Html.Partial”调用的操作抛出的异常。

除了处理 "Application_Error" 中的所有错误外,我们还在 _Layout.cshtml 中定义了以下全局处理程序。这基本上处理了从 html 调用局部视图并且操作抛出异常的情况。如果遇到错误,则会记录数据并显示 toastr 弹出消息。

_Layout.cshtml:

    //Ajax Global Event Loader
    globalVar: ajaxContainer = $('div[id=wrap]');
    $(document).bind("ajaxSend", function () {
        if ($('.k-loading-mask').is("visible") == false) {
            var loader = new ajaxLoader(ajaxContainer, { bgColor: '#fff', duration: 800, opacity: 0.3, classOveride: false });
        }
    }).bind("ajaxComplete", function () {
        if ($('div[class=ajax_overlay]').is(':visible') == true)
            $('div[class=ajax_overlay]').remove();
    }).bind("ajaxError", function (event, jqxhr, settings, thrownError) {
        //debugger;
        $('div[class=ajax_overlay]').remove();
        $('.k-loading-image').remove();
        if ((settings.url.indexOf('Notification/GetActiveNotificationByUserName') < 0)
            && (settings.url.indexOf('LogError/LogData') < 0)) {
            var errorData = 'URL: ' + settings.url + '; Type: ' + settings.type + '; Data: ' + settings.data + '; thrownError: ' + thrownError;
            var model = { errorData: errorData };

            $.ajax({
                url: '@Url.Action("LogData", "LogError")',
                contentType: 'application/json; charset=utf-8',
                type: 'POST',
                dataType: 'html',
                data: JSON.stringify(model)
            })
            .success(function (result) {
                $("div.overlay").hide();
            })
            .error(function (xhr, status) {
                $("div.overlay").hide();
                //alert(status);
            });

            // Set toastr for error notification and display error 1 at a time
            toastr.options.closeButton = true;
            toastr.options.positionClass = 'toast-top-full-width';
            toastr.options.showMethod = 'slideDown';
            toastr.options.hideMethod = 'slideUp';
            toastr.options.showDuration = '1000';
            toastr.options.hideDuration = '1';
            toastr.clear();
            toastr.error('Your request cannot be processed right now. Please try again later!');
        }

        $("div.overlay").hide();
    });