如何从onexception方法重定向到错误页面,而不是返回到c#mvc中的jQuery / Ajax调用

时间:2015-03-13 04:20:48

标签: ajax asp.net-mvc-4

当发生异常时,我试图在onexception方法中重定向到Error页面。但问题是我有Ajax函数,所以即使我在onexception类中重定向,它也不会重定向到错误页面,它总是最终执行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;
            }

        }

这是我的基本控制器onexception方法

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函数只显示Successful消息,它避免了我重定向到错误页面。问题是即使我重定向到onexception方法中的错误页面,最后激发Ajax错误:函数并且它不会重定向到DatabaseException或ApplicationException视图。任何人都可以为此问题提出解决方案。

由于

1 个答案:

答案 0 :(得分:2)

你的问题是当你从ajax函数发帖子时,你控制器中的任何结果总会最终回到成功函数。

您要做的是将网址返回到您的ajax功能,并从那里重定向用户。

在您的控制器中返回以下

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会话中,并在错误视图中以相同的方式使用它