如何在内容处理程序中设置HttpResponse StatusDescription?

时间:2014-02-10 16:12:37

标签: asp.net iis generic-handler

我正在尝试在HTTP响应中设置自定义HTTP状态标头。 E.g:

400 Why do you want to do that

我是通过在StatusDescription中设置HttpResponse的{​​{1}}来实现此目的的,

IHttpHandler

但是在服务器的响应头中,状态行不包含我告诉它包含的内容,而是继续包含默认的StatusDescription:

<%@ WebHandler Language="C#" Class="Foo" %>

using System;
using System.Web;

public class Foo : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
        context.Response.StatusDescription = "Why do you want to do that";

            //Unnecessary; the string already contains this
        context.Response.Status = "400 Why do you want to do that";

        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable { get { return false; } }
}

enter image description here

enter image description here

如何在通用处理程序中更改400 Bad Request 标头的StatusDescription

Bonus Chatter

有三个属性:

  • HttpResponse int StatusCode
  • 400 String StatusDescription
  • Bad Request String Status

设置400 Bad Request会改变StatusCodeStatusDescription

Status

它也有另一种方式:

                    StatuCode  StatusDescription  Status              
==================  =========  =================  ====================
initial             200        OK                 200 OK
StatusCode=400;
                    400        Bad Request        400 Bad Request
StatusDescription = "Too much want";
                    400        Too much want      400 Too much want

和另一种方式

                    StatuCode  StatusDescription  Status              
==================  =========  =================  ====================
initial             200        OK                 200 OK
StatusDescription="Brilliant";
                    200        Brilliant          200 Brilliant
StatusCode=451;
                    451        Brilliant          451 Brilliant

Bonus Bonus Chatter

来自RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1

  

6.1.1状态代码和原因短语

     

为HTTP / 1.1定义的数字状态代码的各个值以及相应的Reason-Phrase的示例集如下所示。这里列出的原因只是建议 - 它们可以被当地的等价物替换,而不会影响协议。

0 个答案:

没有答案