如何响应.在IIS7.5上写?

Ian*_*oyd 1 asp.net iis-7.5

我正在尝试写出对客户的回复:

response.StatusCode = (int)HttpStatusCode.BadRequest;
response.ClearContent();
response.Write(String.Format(
      "<!doctype html>"+CRLF+
      "<html>" + CRLF +
      "<head><title>{0}</title></head>" + CRLF +
      "<body><h1>{0}</h1>"+CRLF+
      "{1}"+CRLF+
      "</body>" + CRLF +
      "</html>", 
      response.Status, "The grob must be in the frobber."));
response.Flush();
response.End();
Run Code Online (Sandbox Code Playgroud)

在localhost上运行时代码工作正常(Visual Studio(2010(Windows 7(专业版(64位)))))开发Cassini Web服务器:

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 17 Jul 2012 15:56:42 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: text/html
Connection: Close

<!doctype html>
<html>
<head><title>400 Bad Request</title></head>
<body><h1>400 Bad Request</h1>
The grob must be in the frobber.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

但是当我将网站部署到运行IIS7.5的Windows Server 2008 R2时,相同的代码不起作用:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 17 Jul 2012 15:57:44 GMT
Content-Length: 11

Bad Request
Run Code Online (Sandbox Code Playgroud)

我如何Response.Write从IIS 执行?

maf*_*fue 5

默认情况下,如果代码> = 400,IIS 7将更改响应 http://msdn.microsoft.com/en-us/library/ms690497(v=vs.90).aspx

但是你可以在httpErrors元素中改变它system.webServer.

<httpErrors existingResponse="PassThrough" />
Run Code Online (Sandbox Code Playgroud)

编辑:CustomErrors如果您正在使用它,这将打破.你可能最好在没有响应的情况下返回400,然后设置web.config以重定向到400个错误的自定义页面.

<customErrors mode="On">
    <error statusCode="400" redirect="Frobber.htm" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)