asp.net中Application_Error()如何知道请求是否是ajax
如何在Application_Error()中知道asp.net中的请求是否是ajax
我想在Application_Error()中处理应用程序错误。如果请求是ajax并且抛出一些异常,则将错误写入日志文件并返回json数据其中包含客户端的错误提示。 否则,如果请求是同步的并且抛出一些异常,则将错误写入日志文件,然后重定向到错误页面。
但现在我无法判断请求是哪一种。我想从标头获取“X-Requested-With”,不幸的是标头的键不包含“X-Requested-With”键,为什么?
How to know if the request is ajax in asp.net in Application_Error()
I want to handle app error in Application_Error().If the request is ajax and some exception is thrown,then write the error in log file and return a json data that contains error tips for client .
Else if the request is synchronism and some exception is thrown ,write the error in log file and then redirect to a error page.
but now i cant judge which kind the request is . I want to get "X-Requested-With" from header ,unfortunately keys of headers don't contain "X-Requested-With" key ,why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对请求标头的测试应该有效。例如:
在
Application_Error
: 中,然后发送一些 Ajax 请求:
Testing for the request header should work. For example:
and in
Application_Error
:and then send some Ajax request:
您还可以将 Context.Request(类型为 HttpRequest)包装在 HttpRequestWrapper 中,其中包含 IsAjaxRequest 方法。
You can also wrap the Context.Request (of the type HttpRequest) in a HttpRequestWrapper which contains a method IsAjaxRequest.
可以在客户端 ajax 调用中添加自定义标头。请参阅http://forums.asp.net/t/1229399.aspx/1
尝试在服务器中查找此标头值。
it is possible to add custom headers in the client side ajax call. Refer http://forums.asp.net/t/1229399.aspx/1
Try looking for this header value in the server.
你可以用这个。
You could use this.