如何在 ASP.Net 中发送状态代码 500 并仍然写入响应?

发布于 2024-10-09 03:56:19 字数 1009 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居人 2024-10-16 03:56:19

Context.Response.TrySkipIisCustomErrors = true

Context.Response.TrySkipIisCustomErrors = true

战皆罪 2024-10-16 03:56:19

我过去使用过以下内容,并且能够使用 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));  
        }
    }

I have used the following in the past and been able to throw a 503 error with a custom message using the code shown below in the Page_Load method. I use this page behind a load balancer as the ping page for the load balancer to know if a server is in service or not.

Hope this helps.

        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));  
        }
    }
迷离° 2024-10-16 03:56:19

您可能希望设置一个自定义错误页面(可通过 web.config 配置)。您可以在会话中跨请求存储内容(或通过替代机制),然后将 ASP.NET 配置为显示自定义错误页面,该页面又显示您的自定义输出。

不过,请注意:如果 500 是由于应用程序的基本问题(即 StackOverflowException)引起的,并且您尝试显示依赖于 asp.net 的页面(即 MyCustomErrors.aspx),则可能会出现以下情况:一个循环。

有关详细信息,请查看此页面

You may wish to set a customErrors page (configurable via the web.config). You can store your content across requests in Session (or via an alternative mechanism) then have asp.net configured to display the custom error page which in-turn displays your custom output.

A word of caution, though: If the 500 is being caused because of a fundamental problem with the application (i.e. StackOverflowException) and you try to display a page that depends on asp.net (i.e. MyCustomErrors.aspx), you may end up in a loop.

For more information, check out this page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文