【发布时间】:2017-10-02 18:53:10
【问题描述】:
在我的 asp.net mvc 应用程序中,我想向用户显示用于引发异常的错误消息。异常发生在 ajax 请求中。我试过这个:
在 Global.asax.cs 文件中,我有全局应用程序错误处理程序:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = System.Web.HttpContext.Current.Server.GetLastError();
// do something so that client side gets the value exception.Message
// I have tried these, but no success
HttpContext.Current.Response.StatusDescription = exception.Message;
HttpContext.Current.Response.StatusCode = 1000; // custom status code
}
在 javascript 中,我有全局 ajaxError 处理程序:
$(document).ajaxError(function (xhr, props) {
// show the message in the exception thrown on the server side
});
我尝试使用 props.statusText、props.responseText 在 JavaScript 中获取异常消息,但它们都没有异常消息。
我的问题:我可以在Application_Error方法中做什么,以便我可以将exception.Message中包含的消息传递给客户端的全局ajaxError函数?请注意,我可以轻松处理发生在 ajax 请求命中的任何特定操作中发生的异常,但我想创建一个全局异常处理程序,这将允许我只使用来自应用程序任何部分的消息引发异常,异常消息会在客户端显示给用户。
我尝试过this,但这并不能解决我的问题,因为我想使用jQuery 全局ajaxError,而不是仅在特定的ajax 请求中处理错误。这可以修改以实现我想要的吗?
【问题讨论】:
标签: javascript c# jquery asp.net asp.net-mvc