Search code examples
c#asp.nethttpasp.net-web-api2http-status-codes

Modify the Http Status Code text


Question

How to modify the Status Code text (description/title)?

Example

For example: I want to change 200 (Ok) to 200 (My Custom Text)

Desciption

I want to create a HTTP response with custom Status Code (unreserved) 431. I want to modify it's text:

  • 200 (OK)
  • 400 (Bad Request)
  • 431 (My message here)

I've tried:

var response = new HttpResponseMessage() 
{
    StatusCode = (HttpStatusCode) 431,
};

response.Headers.Add("Status Code", "431 My custom text"); // This throws error.

Solution

  • Just add ReasonPhrase in initializer :

            var response = new HttpResponseMessage()
            {
                StatusCode = (HttpStatusCode)431,
                ReasonPhrase = "your text"
            };
    

    It defines text of the message that send with the status code