如何从 onexception 方法重定向到错误页面而不是返回到 c# mvc 中的 jQuery/Ajax 调用
How to redirect to error page from onexception method instead of returning to jQuery/Ajax call in c# mvc
我正在尝试在发生异常时重定向到 onexception 方法内的错误页面。但问题是我有 Ajax 函数,所以即使我在 onexception class 内部重定向,它也不会重定向到错误页面,它总是以执行 Ajax 函数结束。请任何人为此提出解决方案。
这是我的控制器方法,当异常抛出时,它会在异常方法上调用基础控制器。
public ActionResult DeleteConfirmed(int id)
{
try
{
string displayMessage = string.Empty;
//Delete ward details and check successfulness of the function and return json result
if (wardManager.DeleteWard(id) == true)
{
displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationErrorMsg);
return Json(new { Message = displayMessage });
}
//Return json result if unsuccessfull
else
{
displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationRemovedMsg);
return Json(new { Message = displayMessage });
}
}
catch (System.Exception ex)
{
throw ex;
}
}
这是我的基本控制器异常方法
protected override void OnException(ExceptionContext filterContext)
{
//Get exception type
System.Type ExceptionType = filterContext.Exception.GetType();
if (ExceptionType.Name == "DbUpdateException")
{
ViewData["ErrorMessage"] = filterContext.Exception.InnerException.Message;
this.View("DatabaseException", filterContext.Exception).ExecuteResult(this.ControllerContext);
}
else
{
ViewData["ErrorMessage"] = filterContext.Exception.Message;
this.View("ApplicationException", filterContext.Exception).ExecuteResult(this.ControllerContext);
}
}
这是我的 Ajax 函数
$.ajax({
type: "POST",
dataType: "json",
jsonpCallback: "onJSONPLoad",
url: '@Url.Action("Delete")',
data: $('#form').serialize(),
success: function (data) {
$('#submit').hide();
TriggerMsg(data.Message);
},
error: function (xhr) {
if (xhr.getResponseHeader('Content-Type').indexOf('application/json') > -1) {
var json = $.parseJSON(xhr.responseText);
alert(json.errorMessage);
}
}
});
我构建这个 Ajax 函数只是为了显示成功的消息,它避免了我重定向到错误页面。问题是即使我重定向到 onexception 方法内的错误页面,最终触发 Ajax error: function 并且它不会重定向到 DatabaseException 或 ApplicationException 视图。任何人都可以为这个问题提出解决方案。
谢谢
你的问题是,当你从 ajax 函数执行 post 时,无论你在控制器中得到什么结果,最终都会返回到成功函数。
你想要做的是 return 一个 url 到你的 ajax 函数,然后从那里重定向用户。
在你的控制器中return以下
result = Json(new { redirect = Url.Action("ActionName", "ControllerName", new { area = "" }) });
然后在您的 javascript 中查找此重定向变量并将 window.location 设置为它
success: function (data) {
if (data.redirect) {
window.location.href = data.redirect;
}
},
如果您还想传递一条消息,那么您可以将其放入来自控制器的 Session["ErrorMessage"] = error
等会话中,并在您的错误视图中以相同的方式使用它
我正在尝试在发生异常时重定向到 onexception 方法内的错误页面。但问题是我有 Ajax 函数,所以即使我在 onexception class 内部重定向,它也不会重定向到错误页面,它总是以执行 Ajax 函数结束。请任何人为此提出解决方案。
这是我的控制器方法,当异常抛出时,它会在异常方法上调用基础控制器。
public ActionResult DeleteConfirmed(int id)
{
try
{
string displayMessage = string.Empty;
//Delete ward details and check successfulness of the function and return json result
if (wardManager.DeleteWard(id) == true)
{
displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationErrorMsg);
return Json(new { Message = displayMessage });
}
//Return json result if unsuccessfull
else
{
displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationRemovedMsg);
return Json(new { Message = displayMessage });
}
}
catch (System.Exception ex)
{
throw ex;
}
}
这是我的基本控制器异常方法
protected override void OnException(ExceptionContext filterContext)
{
//Get exception type
System.Type ExceptionType = filterContext.Exception.GetType();
if (ExceptionType.Name == "DbUpdateException")
{
ViewData["ErrorMessage"] = filterContext.Exception.InnerException.Message;
this.View("DatabaseException", filterContext.Exception).ExecuteResult(this.ControllerContext);
}
else
{
ViewData["ErrorMessage"] = filterContext.Exception.Message;
this.View("ApplicationException", filterContext.Exception).ExecuteResult(this.ControllerContext);
}
}
这是我的 Ajax 函数
$.ajax({
type: "POST",
dataType: "json",
jsonpCallback: "onJSONPLoad",
url: '@Url.Action("Delete")',
data: $('#form').serialize(),
success: function (data) {
$('#submit').hide();
TriggerMsg(data.Message);
},
error: function (xhr) {
if (xhr.getResponseHeader('Content-Type').indexOf('application/json') > -1) {
var json = $.parseJSON(xhr.responseText);
alert(json.errorMessage);
}
}
});
我构建这个 Ajax 函数只是为了显示成功的消息,它避免了我重定向到错误页面。问题是即使我重定向到 onexception 方法内的错误页面,最终触发 Ajax error: function 并且它不会重定向到 DatabaseException 或 ApplicationException 视图。任何人都可以为这个问题提出解决方案。
谢谢
你的问题是,当你从 ajax 函数执行 post 时,无论你在控制器中得到什么结果,最终都会返回到成功函数。
你想要做的是 return 一个 url 到你的 ajax 函数,然后从那里重定向用户。
在你的控制器中return以下
result = Json(new { redirect = Url.Action("ActionName", "ControllerName", new { area = "" }) });
然后在您的 javascript 中查找此重定向变量并将 window.location 设置为它
success: function (data) {
if (data.redirect) {
window.location.href = data.redirect;
}
},
如果您还想传递一条消息,那么您可以将其放入来自控制器的 Session["ErrorMessage"] = error
等会话中,并在您的错误视图中以相同的方式使用它