简体   繁体   English

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

[英]How to send a Status Code 500 in ASP.Net and still write to the response?

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. 我有一个ASP.Net单文件Web服务(包含IHttpHandler实现的.ashx文件),它需要能够作为具有500个内部服务器错误状态代码的响应返回错误。 This is a relatively straightforward thing to do in PHP: 这在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: ASP.Net(C#)等效应该是:

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. 相反,IIS拦截500状态代码,删除我写入Response对象的任何内容,并发送调试信息或自定义错误页面,具体取决于应用程序的配置方式。

My question - how can I suppress this IIS behaviour and send error information directly from my IHttpHandler implementation? 我的问题 - 如何抑制此IIS行为并直接从我的IHttpHandler实现发送错误信息?

This app is a port from PHP; 这个应用程序是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. 错误地发送200状态代码的错误不适合模具。

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. 理想情况下,我需要以编程方式控制行为,因为这是我们要分发的SDK的一部分,而没有任何“ 编辑此文件 ”和“ 更改此IIS设置 ”补充说明。

Thanks! 谢谢!

Edit : Sorted. 编辑 :已排序。 Context.Response.TrySkipIisCustomErrors = true was the ticket. Context.Response.TrySkipIisCustomErrors = true是票证。 Wow. 哇。

Context.Response.TrySkipIisCustomErrors = true

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. 我以前使用过以下内容,并且能够使用Page_Load方法中显示的代码在自定义消息中抛出503错误。 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. 我在负载均衡器后面使用此页面作为负载均衡器的ping页面,以了解服务器是否在服务中。

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));  
        }
    }

You may wish to set a customErrors page (configurable via the web.config). 您可能希望设置customErrors页面(可通过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. 您可以在Session中的请求中存储您的内容(或通过替代机制),然后配置asp.net以显示自定义错误页面,该页面反过来显示您的自定义输出。

A word of caution, though: If the 500 is being caused because of a fundamental problem with the application (ie StackOverflowException) and you try to display a page that depends on asp.net (ie MyCustomErrors.aspx), you may end up in a loop. 但需要注意的是:如果500是由于应用程序的基本问题(即StackOverflowException)引起的,并且您尝试显示依赖于asp.net的页面(即MyCustomErrors.aspx),则可能最终一个循环。

For more information, check out this page . 有关更多信息,请查看此页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM