在MVC中使用PartialView进行全局错误处理

时间:2016-09-20 12:41:17

标签: c# asp.net-mvc exception exception-handling try-catch

我查看了Exception handling in ASP.NET MVC上的一篇完美文章,并希望在本文的方法6中实现一个方法,以便在错误和异常情况下为所有其他模式对话重用相同的错误页面。另一方面,当我使用弹出窗口时,我需要在模态对话框中渲染PartialView而不是重定向页面。是否有可能做到这一点?

AJAX致电:

$.ajax({
    type: "POST",
    url: '@Url.Action("Delete", "Person")',
    cache: false,
    dataType: "json",
    data: formdata,

    success: function (response, textStatus, XMLHttpRequest) {
        if (response.success) {
            //display operation result
        }
        else {
            /* At this stage I need to render the Error view as partial without 
            redirecting to Error page on error. The problem at here not directly 
            related to rendering partialview. I need to render the global Error 
            page and reuse it for every error occured on my modal dialog */
            @Html.Partial("~/Views/Home/Error.cshtml", Model)
        }
    }
});

控制器:

[HttpPost]
public JsonResult Delete(int id)
{
    try
    {
        //code omitted for brevity
    }
    catch (Exception  ex)
    {
        return Json(new { success = false, message = "Operation failed." });
    }
}

0 个答案:

没有答案