如何发送状态code 500在ASP.Net,仍然写入响应? [英] How to send a Status Code 500 in ASP.Net and still write to the response?

查看:254
本文介绍了如何发送状态code 500在ASP.Net,仍然写入响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net单文件Web服务(含的IHttpHandler 实施 ashx的文件)这就需要能够返回错误与 500内部服务器错误状态codeS响应。这是PHP做一个相对简单的事情:

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";

在ASP.Net(C#)等价应该是:

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

当然,这并不能达到预期效果;相反,IIS截获500状态code,无论象垃圾一样清除我写的响应对象,无论是发送调试信息或自定义错误页,这取决于如何应用程序配置。

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.

我的问题 - 我怎么能燮preSS此IIS的行为而直接从我的的IHttpHandler 实施发送错误信息

My question - how can I suppress this IIS behaviour and send error information directly from my IHttpHandler implementation?

这个程序是从PHP的端口;客户端已经写好,所以我基本上坚持了这个天赋。发送错误200状态code可悲的是不适合的模具。

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.

在理想情况下,我需要通过编程控制的行为,因为这是我们想没有任何分发SDK的一部分的编辑这个文件的和更改此IIS设置的补充说明。

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.

谢谢!

修改:排序。 Context.Response.TrySkipIisCustomErrors = TRUE 是票。哇。

Edit: Sorted. Context.Response.TrySkipIisCustomErrors = true was the ticket. Wow.

推荐答案

Context.Response.TrySkipIisCustomErrors = TRUE

这篇关于如何发送状态code 500在ASP.Net,仍然写入响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆