我已经阅读了很多关于MVC中的错误处理的内容(特别是关于应用HandleError属性以及关于在Global.asax上使用Application_Error)。我有兴趣优雅地处理以下类型的异常:
目前我的应用程序的行为方式如下
此逻辑适用于在呈现父视图或父视图本身的控制器内抛出的错误。这个逻辑的缺点是,当一个Child Action内部抛出一个错误,它应该呈现一个PartialView,整个页面变得无法使用。这是因为黄色错误屏幕或自定义错误页面占用了整个页面,并且不允许用户查看网页的其他部分。
我想做/知道的是否有可能:
答案 0 :(得分:0)
处理这种特殊情况:
"当一个Child Action内部抛出错误时应该呈现一个错误 PartialView整个页面变得无法使用。"
您可以为" 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();
});