A-Sharabiani
A-Sharabiani

Reputation: 19329

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:

I've tried:

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

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

Upvotes: 3

Views: 7297

Answers (3)

Steve Smith
Steve Smith

Reputation: 2270

If you're returning just a status code and a message, you can do something like:

return this.Ok("My Custom Text");

or

return this.Unauthorized("Invalid Pin");

This will change the responseText.

Upvotes: 0

gnalck
gnalck

Reputation: 972

One way to solve this issue is to skip the validation of the header you add. This can be done with the TryAddWithoutValidation method.

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

response.Headers.TryAddWithoutValidation ("Status Code", "431 My custom text");

Upvotes: 2

Vladimir
Vladimir

Reputation: 1390

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

Upvotes: 8

Related Questions