如何在 ASP.Net 中发送状态代码 500 并仍然写入响应?
我有一个 ASP.Net 单文件 Web 服务(一个包含 IHttpHandler
实现的 .ashx
文件),它需要能够返回错误作为 500 的响应内部服务器错误状态代码。在 PHP 中这是一件相对简单的事情:
header("HTTP/1.1 500 Internal Server Error");
header("Content-Type: text/plain");
echo "Unable to connect to database on $dbHost";
ASP.Net (C#) 的等效项应该是:
Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Context.Response.ContentType = "text/plain";
Context.Response.Write("Unable to connect to database on " + dbHost);
当然,这不会按预期工作;相反,IIS 会拦截 500 状态代码,丢弃我写入 Response 对象的所有内容,并发送调试信息或自定义错误页面,具体取决于应用程序的配置方式。
我的问题 - 如何抑制此 IIS 行为并直接从我的 IHttpHandler
实现发送错误信息?
这个应用程序是 PHP 的移植版;客户端已经写好了,所以我基本上坚持这个规范。遗憾的是,发送带有 200 状态代码的错误并不符合这种模式。
理想情况下,我需要以编程方式控制行为,因为这是我们想要分发的 SDK 的一部分,没有任何“编辑此文件”和“更改此 IIS 设置”补充指示。
谢谢!
编辑:已排序。 Context.Response.TrySkipIisCustomErrors = true
就是票证。哇。
I have an ASP.Net single-file web service (a .ashx
file containing an IHttpHandler
implementation) which needs to be able to return errors as responses with 500 Internal Server Error status codes. This is a relatively straightforward thing to do in PHP:
header("HTTP/1.1 500 Internal Server Error");
header("Content-Type: text/plain");
echo "Unable to connect to database on $dbHost";
The ASP.Net (C#) equivalent should be:
Context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Context.Response.ContentType = "text/plain";
Context.Response.Write("Unable to connect to database on " + dbHost);
Of course, this doesn't work as expected; instead, IIS intercepts the 500 status code, trashes whatever I've written to the Response
object, and sends either debug info or a custom error page, depending on how the app is configured.
My question - how can I suppress this IIS behaviour and send error information directly from my IHttpHandler
implementation?
This app is a port from PHP; the client-side is already written, so I'm essentially stuck with this spec. Sending errors with a 200 status code sadly doesn't fit the mould.
Ideally, I need to control the behaviour programmatically, because this is part of an SDK we want to distribute without any "edit this file" and "change this IIS setting" supplementary instructions.
Thanks!
Edit: Sorted. Context.Response.TrySkipIisCustomErrors = true
was the ticket. Wow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
我过去使用过以下内容,并且能够使用 Page_Load 方法中下面显示的代码抛出带有自定义消息的 503 错误。我在负载均衡器后面使用此页面作为负载均衡器的 ping 页面,以了解服务器是否正在服务。
希望这有帮助。
protected void Page_Load(object sender, System.EventArgs e)
{
if (Common.CheckDatabaseConnection())
{
this.LiteralMachineName.Text = Environment.MachineName;
}
else
{
Response.ClearHeaders();
Response.ClearContent();
Response.Status = "503 ServiceUnavailable";
Response.StatusCode = 503;
Response.StatusDescription= "An error has occurred";
Response.Flush();
throw new HttpException(503,string.Format("An internal error occurred in the Application on {0}",Environment.MachineName));
}
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Context.Response.TrySkipIisCustomErrors = true
Context.Response.TrySkipIisCustomErrors = true