- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用asp.net mvc开发应用程序
我正在发送ajax发布请求。
操作过程中发生错误时,我得到500个状态代码。
这种情况是正确的,但是web.config文件中的代码重定向到错误页面。
这是我的代码
public class ErrorHandler : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
var response = filterContext.RequestContext.HttpContext.Response;
filterContext.ExceptionHandled = true;
response.StatusCode = (int)HttpStatusCode.InternalServerError;
filterContext.Result = new JsonResult
{
Data = new {
success = false
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom">
<remove statusCode="500"/>
<error statusCode="500" path="/common/error" responseMode="Redirect"/>
</httpErrors>
最佳答案
一点建议
发生异常时,不要一直将response.StatusCode
设置为HttpStatusCode.InternalServerError
。请尝试并更具体一些,这样您就不会总是返回500。如果您返回另一个更具体的错误代码,则根据web.config
不会发送重定向。例如,如果错误是由于ajax请求了具有特定ID的用户而该用户不存在,则返回错误代码404。
现在要解决您的问题,您可以执行以下操作:
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
CallbackException error = new CallbackException();
error.message = errorMessage;
error.isCallbackError = true;
error.stackTrace = stackTrace;
JSONSerializer Serializer = new JSONSerializer();
string result = Serializer.Serialize(error);
var response = filterContext.RequestContext.HttpContext.Response;
response.ContentType = ControlResources.STR_JsonContentType;
response.TrySkipIisCustomErrors = true;
response.StatusCode = 500;
response.Write(Result);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
else
{
// The code in your answer goes here
}
关于asp.net-mvc - 我如何使用HttpResponseMessage HttpStatusCode分隔web.config httpErrors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42472886/
我收到“HTTP 错误 500:内部服务器错误”响应,但我仍想读取错误 HTML 中的数据。 使用 Python 2.6,我通常使用以下方法获取页面: import urllib2 url = "ht
我的 API 提供商在 response.body 中发送错误消息作为 ReadableStream为纯文本,我需要让管理员在休息时将这些消息显示给最终用户。 我打算这样做: import HttpE
我正在尝试从Youtube数据API获取视频数据,但是在经过一定数量的请求调用(大约900次)后,我遇到了(500)后端错误,我也试图等待15分钟的间隔,持续4到5次,但仍然抛出同样的错误 错误: b
刚开始学Python,就遇到了这个问题。真正解析来自亚马逊的价格并将其打印到控制台。 这是我的代码: import requests, bs4 def getAmazonPrice(productUr
刚开始学Python,就遇到了这个问题。真正解析来自亚马逊的价格并将其打印到控制台。 这是我的代码: import requests, bs4 def getAmazonPrice(productUr
测试一些使用 urllib2 的代码, 我想养一个 HTTPError以确保它得到正确处理。 HTTPError 有一个构造函数: def __init__(self, url, code, msg,
如果是 404,我想捕获带有额外信息的 urllib2.HTTPError: try: data = urlopen(url) except HTTPError, e: # Python 2
我在 IIS 7 上本地运行的 Asp.Net 4 中有一个 Web 应用程序。 我需要为 IIS 的默认页面显示一个自定义页面 (404) 和一个 500。 在 Web.Config 中使用这个 h
我在为 cherrypy.HTTPError 提供自定义响应处理时遇到问题.我希望在响应正文中显示的唯一内容是 JSON 编码的 dict(这是 REST API)。源代码似乎表明HTTPError.
我在 web.config 中有这个: 但是 IIS 仍然显示内置的错误页面。 有什么想法吗? 最佳答案 这就是我使用它的方式,它对我有用,除了 subStatusCode
如何在 Angular 服务中模拟 HTTP 错误响应?我经常需要处理不同的 HTTP 错误代码,有时我需要实现解决方案,但后端还没有准备好。如何从后端模拟错误? 示例代码 public getD
在我的Google帐户(不是主要帐户)上,我创建了OAuth凭据,可以访问CMS及其 channel 。使用此凭据,我可以从YouTube Analytics(分析)API中获取数据,因此这里没有问题
我已将以下内容添加到配置文件中。 ... 但是,我似乎仍然得到带有黄色背景和堆栈跟踪的默认页面。我试过在 system.web 中注释掉用于错误处理和添加/删除
当我尝试运行 conda update numpy 时出现以下错误(也在运行 conda update conda 时) Fetching package metadata ............C
我正在尝试找出一种使用 ASP.NET WebApi 返回具有非 200 返回代码的自定义负载的方法。基本上我想拥有 {"status":{"code":"40001", "message": "Fi
我在实现 MyFaces 2.0 时使用 JSF我有 2 个 jsf 页面 login.xhtml 和 register.xhtml。 登录.xhtml: System CollD
如何返回 HttpError(自己的、格式漂亮的前端错误消息)然后使用方便的 IHttpActionResult? public IHttpActionResult Delete(Guid id) {
我正在用 python 尝试“Hello world” import webapp2 class MainHandler(webapp2.RequestHandler): def get(
如何获取 中设置的 errorMode 属性的值? web.config 中的元素? 我正在尝试在 ASP.NET Web 应用程序中实现一些“ self 诊断”。当应用程序启动时,它会运行 web
我正在尝试用 ruby 编写一个简单的网络抓取代码。它一直工作到第 29 个 url,然后我收到此错误消息: C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:346:i
我是一名优秀的程序员,十分优秀!