Javascript 向客户端显示异常消息
在我的asp.net mvc应用程序中,我想向用户显示用于引发异常的错误消息。该异常发生在ajax请求中。我试过这个: 在Global.asax.cs文件中,我有全局应用程序错误处理程序:Javascript 向客户端显示异常消息,javascript,c#,jquery,asp.net,asp.net-mvc,Javascript,C#,Jquery,Asp.net,Asp.net Mvc,在我的asp.net mvc应用程序中,我想向用户显示用于引发异常的错误消息。该异常发生在ajax请求中。我试过这个: 在Global.asax.cs文件中,我有全局应用程序错误处理程序: protected void Application_Error(object sender, EventArgs e) { Exception exception = System.Web.HttpContext.Current.Server.GetLastError(); // do s
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
}
$(document).ajaxError(function (xhr, props) {
// show the message in the exception thrown on the server side
});
在javascript中,我有全局ajaxError处理程序:
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
}
$(document).ajaxError(function (xhr, props) {
// show the message in the exception thrown on the server side
});
我尝试使用JavaScript中的props.statusText
,props.responseText
,获取异常消息,但他们都没有异常消息
我的问题:我可以在应用程序\u Error
方法中做些什么,以便将异常中包含的消息发送到客户端的全局ajaxError
函数?请注意,我可以很容易地处理ajax请求命中的任何特定操作中发生的异常,但我想创建一个全局异常处理程序,它允许我只抛出一个异常,并从应用程序的任何部分发送一条消息,异常消息将在客户端显示给用户
我已经尝试过了,但这并不能解决我的问题,因为我想使用jqueryglobalajaxError
,而不是只处理特定ajax请求中的错误。可以修改它以实现我想要的吗?您可以创建一个basecontroller并覆盖OneException
方法
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//your existing code to log errors here
filterContext.ExceptionHandled = true;
if (filterContext.HttpContext.Request.Headers["X-Requested-With"]
== "XMLHttpRequest")
{
filterContext.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
Error = true,
Message = filterContext.Exception.Message
}
};
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.ExceptionHandled = true;
}
else
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{{"controller", "Error"}, {"action", "Index"}});
}
}
}
让你所有的控制器都继承自此
public class HomeController : BaseController
{
}
因此,每当控制器中发生异常时,就会执行这个OnException
方法,如果它是ajax请求,就会返回一个具有以下结构的json响应
{
Error : true,
Message : "The message from the exception caught"
}
现在在javascript中,连接全局ajaxError
事件,您可以读取来自服务器的响应并将其解析为js对象,然后读取消息
属性
$(document).ready(function() {
$(document).ajaxError(function (event, request, settings) {
var d = JSON.parse(request.responseText);
alert("Ajax error:"+d.Message);
});
});
我已经尝试过一些类似的代码,但没有成功。也许我用的方法不对。安威,我有一个问题,有了这段代码,我是否需要消除Global.asax中的应用程序错误?我不会使用Global.asax。当您决定在未来的项目中使用ASPNETcore时,您将无法再使用它。但是,受保护的override void OneException(ExceptionContext filterContext){}将在控制器中使用。