我们有一个处理数据库请求的 C# AJAX 服务器,我们希望在异常时返回 400/403/500 状态代码,以便我们可以了解客户端发生了什么。然而,我们得到的只是响应正文中的“Bad Request”。
例如,如果 ADO.Net 驱动程序返回此消息:
Invalid length parameter passed to the LEFT or SUBSTRING function. Cannot insert the value NULL into column '--COLUMN--', table '--DATABASE.SCHEMA.TABLE--'; column does not allow nulls. INSERT fails. The statement has been terminated.
然后我们的 AJAX 服务器简单地返回这个文本:
Bad Request
我已经查看了 Chrome 中的完整响应 header ,其中没有任何类似错误消息的内容。这是我们用来发回错误的代码:
catch (Exception ex)
{
//Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.SC_BAD_REQUEST;
Response.Write(ex.Message);
Response.Flush();
}
在与此服务器等效的 Java/JSP 中,我们使用以下代码获得正确的行为:
} catch (Exception e) {
response.sendError(response.SC_BAD_REQUEST, e.getClass() + " : " + e.getMessage());
}
最佳答案
有些事情可能会出错,但我敢打赌您正在 IIS7 上运行,并且它的默认值导致它重写错误。试试 TrySkipIisCustomErrors httpErrors 上的属性和 existingResponse
属性配置设置。
关于c# - 如何使用 C#/ASP.Net 在 responseText 中传回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433792/