BanksySan
BanksySan

Reputation: 28500

HttpResult(HttpStatusCode.NoContent) creates response "OK"

I am using .NET MVC.

When I return new HttpResult(HttpStatusCode.NoContent); the object that is created has a status code = 200:

{ServiceStack.Common.Web.HttpResult}
    AllowsPartialResponse: false
    ContentType: null
    FileInfo: null
    Headers: Count = 0
    IsPartialRequest: false
    Options: Count = 0
    RequestContext: null
    Response: NoContent
    ResponseFilter: {ServiceStack.Common.Web.HttpResponseFilter}
    ResponseStream: null
    ResponseText: null
    Status: 200
    StatusCode: OK
    StatusDescription: null
    Template: null
    View: null

Erm.. what?

Upvotes: 4

Views: 3029

Answers (1)

Mike
Mike

Reputation: 1837

You need to specify a status description in the constructor if you want to use the overloaded constructor that takes a HttpStatusCode

If you look at the code for HttpResult you can see that the constructor you are calling is this:

public HttpResult(object response) : this(response, null) {}

The HttpStatusCode object you are passing in is actually being used as the response (any object can be a response). There is another constructor that you should be using here:

public HttpResult(HttpStatusCode statusCode, string statusDescription)

i.e.

new HttpResult(HttpStatusCode.NoContent, "No Content");

Upvotes: 6

Related Questions