c# - 向客户端发送状态码 500 时页面未呈现

标签 c# asp.net iis-7 http-status-codes

我有一个页面(通用处理程序),我想在该页面上将状态代码 500 返回给客户端以指示出现问题。我这样做:

Response.StatusCode = 500;
Response.StatusDescription = "Internal Server Error";

与此同时,我会呈现一条友好的消息,告诉用户出了点问题。 但是我没有看到我的消息,而是收到了默认的 IIS 消息,内容如下:

Server Error 500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.

如果我进入 IIS 并删除 500 的错误页面,我会收到以下消息:

The page cannot be displayed because an internal server error has occurred.

它在 IIS6 中按预期工作,但在 IIS7 中则不然。我应该怎么做才能让它在 IIS7 中工作?

最佳答案

你还需要一行来绕过 IIS7 接管(基于你设置的 500 错误代码):

Response.TrySkipIisCustomErrors = true;

引用:HttpResponse.TrySkipIisCustomErrors

关于c# - 向客户端发送状态码 500 时页面未呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3288797/

相关文章:

c# - 无法将参数值从 DropDownList 转换为字符串

c# - 想安装 DNN 7 但出现 Configuration Error 错误

iis - 将 Kinect RGB 或深度相机流式传输到互联网

c# - C# 中的委托(delegate)定义

c# - 将二进制文件保存为文件

asp.net - 在 ASP.NET MVC 应用程序中从 Amazon SES 发送电子邮件

asp.net - 如何将成员资格表与 Entity Framework 集成? ASP.net

asp.net - IIS7 404错误

c# - 选择ListView的特定列并在C#.net中的新消息框中打印

c# - 对 Enumerable 调用 .ToArray 会破坏 Enumerable