报告 Ajax 调用 MVC 中的 PartialView 方法的错误
如果正常的页面加载错误,我可以通过 Error
视图和 HandleErrorInfo
模型向用户报告异常详细信息。
如果 ajax
调用期望 Json
结果错误,我可以显式处理错误并将详细信息传递给客户端:
public JsonResult Whatever()
{
try
{
DoSomething();
return Json(new { status = "OK" });
}
catch (Exception e)
{
return Json(new { status = "Error", message = e.Message });
}
}
所以,我的问题是,我看不到任何方法报告 Ajax 调用返回部分视图的操作的错误详细信息。
$.ajax({
url: 'whatever/trevor',
error: function (jqXHR, status, error) {
alert('An error occured: ' + error);
},
success: function (html) {
$container.html(html);
}
});
这只会报告一个 Http 错误代码(例如内部服务器错误),这对客户端没有帮助。是否有一些巧妙的技巧可以传递成功的 PartialView (html) 结果或错误消息?
从 ViewResult
显式评估 html 并将其作为 Json
对象的一部分与状态一起返回似乎太难闻了。是否有处理这种情况的既定模式?
If a normal page load errors I can report the exception details to the user via the Error
view and HandleErrorInfo
model.
If an ajax
call expecting a Json
result errors, I can explicitly handle the error and pass details to the client:
public JsonResult Whatever()
{
try
{
DoSomething();
return Json(new { status = "OK" });
}
catch (Exception e)
{
return Json(new { status = "Error", message = e.Message });
}
}
So, my problem, I can't see any way to report error details from an Ajax call to an action returning a partial view.
$.ajax({
url: 'whatever/trevor',
error: function (jqXHR, status, error) {
alert('An error occured: ' + error);
},
success: function (html) {
$container.html(html);
}
});
This will only report an Http error code (e.g. Internal Server Error) which is not helpful to the client. Is there some clever trick to pass either a successful PartialView (html) result or an error message?
Explicitly evaluating the html from the ViewResult
and returning it as part of a Json
object along with a status seems too smelly. Is there an established pattern for handling this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器操作:
然后我们为应用程序定义一个全局错误处理程序:
以下是全局错误处理程序中使用的 ErrorsController 的样子。也许我们可以为 404 和 500 操作定义一些自定义视图:
然后我们可以订阅 全局错误处理程序 对于所有 AJAX 错误,这样我们就不必为所有 AJAX 请求重复此错误处理代码,但如果我们愿意,我们可以重复它:
最后,我们向控制器操作发出 AJAX 请求,我们希望该操作将返回 HTML 部分在这个 案件:
Controller action:
Then we define a Global error handler for our application:
Here's how the ErrorsController used in the global error handler could look like. Probably we could define some custom views for the 404 and 500 actions:
Then we could subscribe for a global error handler for all AJAX errors so that we don't have to repeat this error handling code for all AJAX requests but if we wanted we could repeat it:
And finally we fire an AJAX request to the controller action that we hope will return an HTML partial in this case:
创建 HandleErrorAttribute (JsonHandleErrorAttribute ?) 的重写版本并在 json 操作中添加 [JsonHandleError]。
看看 asp.net mvc [handleerror] [authorize] 中的 AjaxAuthorizeAttribute 和 JsonResult ?
Create an overriden version of HandleErrorAttribute (JsonHandleErrorAttribute ?) and add [JsonHandleError] on your json action.
Have a look at AjaxAuthorizeAttribute in asp.net mvc [handleerror] [authorize] with JsonResult?