爲了處理這種特殊情況:
「當錯誤被拋出一個子操作,其應該呈現 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();
});
我有類似的設置,從未發生過任何問題。我們全部使用局部視圖。所以很想知道你是否可以提供一個示例代碼,我可以使用我的設置來測試這個代碼:「當一個錯誤被引入到一個應該呈現PartialView的子Action中時,整個頁面變得不可用。」? – SBirthare 2015-03-31 04:46:50
我想你可以在你的設置中看到答案,全球。ASAX是全球性的應用程序,如果你要處理,你需要改變你的應用程序設置的應用程序內部的錯誤,並且設置了使用您的母版,並根據你想在客戶端什麼插在一個位置上的錯誤頁面有權訪問。您可以呈現一個消息等的局部視圖....發生 – 2015-03-31 07:45:19
@SBirthare只需添加代碼'拋出新的異常()'行內的控制器操作或局部視圖內的任何地方,看看有什麼。 @JackM我已經準備好了。全局錯誤頁面已經定義並且用戶被重定向到它。所以,整個頁面被我的自定義屏幕代替 – 2015-03-31 15:43:10