I am trying to set a custom HTTP status header in an HTTP response. E.g.:
400 Why do you want to do that
I am doing this by setting the StatusDescription
of an HttpResponse
in an IHttpHandler
, in
<%@ 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; } }
}
But in the response headers from the server the status line doesn't contain what i told it to contain, and instead continues to contain the default StatusDescription:
400 Bad Request
How do change the StatusDescription
of an HttpResponse
header when in a generic handler?
Bonus Chatter
There are three properties:
int StatusCode
:400
String StatusDescription
:Bad Request
String Status
:400 Bad Request
Setting the StatusCode
will alter the StatusDescription
and 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
It also works the other way:
StatuCode StatusDescription Status
================== ========= ================= ====================
initial 200 OK 200 OK
StatusDescription="Brilliant";
200 Brilliant 200 Brilliant
StatusCode=451;
451 Brilliant 451 Brilliant
and the other way
StatuCode StatusDescription Status
================== ========= ================= ====================
initial 200 OK 200 OK
Status="451 Brilliant"
451 Brilliant 451 Brilliant
Bonus Bonus Chatter
From RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1:
6.1.1 Status Code and Reason Phrase
The individual values of the numeric status codes defined for HTTP/1.1, and an example set of corresponding Reason-Phrase's, are presented below. The reason phrases listed here are only recommendations -- they MAY be replaced by local equivalents without affecting the protocol.