簡體   English   中英

處理MVC視圖與PartialView時出錯

[英]Error handling MVC Views vs PartialView

我已經閱讀了很多有關MVC中的錯誤處理的信息(特別是有關應用HandleError屬性以及有關在Global.asax上使用Application_Error的信息)。 我對優雅地處理以下類型的異常感興趣:

  • 控制器內部引發的異常
  • 在視圖中執行數據綁定時引發的異常。

目前,我的應用程序的行為如下

  1. 控制器中引發的所有異常均未處理。 它們到達Global.asax的Application_Error方法
  2. 視圖中的所有異常均未處理,並到達Global.asax的Application_Error方法
  3. 一旦進入Application_Error方法,我將記錄該異常,並確定該應用程序是在本地運行還是在遠程運行。 如果是這樣,我向用戶顯示黃色屏幕,或執行Response.Redirect到自定義錯誤頁面。

對於在渲染父視圖或父視圖本身的控制器中引發的錯誤,此邏輯正確起作用。 這種邏輯的缺點是,當在子動作中引發錯誤時,該動作應呈現PartialView,整個頁面將變得無法使用。 這是因為黃色錯誤屏幕或自定義錯誤頁面占據了整個頁面,並且不允許用戶查看網頁的其他部分。

我想/知道的是是否可以:

  1. 在部分視圖內顯示黃色錯誤屏幕,但正確呈現頁面的其余部分。
  2. 將用戶重定向到部分視圖錯誤頁面,該頁面允許其余頁面保持可用。

要處理此特定情況:

“當在子動作中引發錯誤時,該動作應導致PartialView整個頁面變得不可用。”

您可以為“ ajaxError”定義一個全局Ajax事件處理程序,以處理使用“ @ Html.Partial”調用的操作引發的異常。

除了處理“ Application_Error”中的所有錯誤外,我們還在_Layout.cshtml中定義了以下全局處理程序。 這基本上可以處理從html調用部分視圖而該操作引發異常的情況。 如果遇到錯誤,將記錄數據並顯示烤面包機彈出消息。

_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();
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM