user2868596
user2868596

Reputation: 1

Web APi Void, IIS express content type, IIS no content Type

Okay, so I have a web api controller with put action and a return type of void. When I run it using VS's builtin iisepxress and call it, I get back a 204 as expected. The here are the headers:

Cache-Control   no-cache
Connection  close
Content-Type    text/html
Date    Thu, 10 Oct 2013 19:33:43 GMT
Expires -1
Pragma  no-cache
Server  Microsoft-IIS/8.0
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET

When I put the exact same code in our sbx environment, I get a 204, but with the following headers:

Cache-Control   no-cache
Date    Thu, 10 Oct 2013 19:39:59 GMT
Expires -1
Pragma  no-cache
Server  Microsoft-IIS/7.5
X-AspNet-Version    4.0.30319
X-Identifier    17253
X-Powered-By    ASP.NET

The pertinent difference being the lack of contentType in the second one.

The problem this creates is that in firefox (and IE I think) it defaults to xml, tries to parse it and fails.

I know how to fix this by setting my contentType in my web api controller, but that doesn't seem like the best fix to me.

So, what I'm asking is what setting difference in IIS might be causing this?

Thanks

Note: My url looks like this /foo/bar/2 so it isn't mimetype.

Upvotes: 0

Views: 729

Answers (3)

pablo roman
pablo roman

Reputation: 1

I use this:

Return StatusCode(HttpStatusCode.NoContent);

Upvotes: 0

diego nombela
diego nombela

Reputation: 1

That will solve the isssue:

    protected internal virtual IHttpActionResult NoContent()
    {
        HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.NoContent) {Content = new StringContent(string.Empty, Encoding.UTF8)};
        return this.ResponseMessage(responseMsg);
    }

But still doesn't explain why IIS is adding by default: Content-Type text/html

Or even better how to remove it using web.config or IIS config.

Upvotes: 0

LordHits
LordHits

Reputation: 5083

If your service is responding with a 204, the response should not contain a message-body. This is by spec. I can only assume you are responding with something in your message body.

Your response from the API method should be like this:

return new HttpResponseMessage { StatusCode = System.Net.HttpStatusCode.NoContent }

Edit. I noticed you mentioned you return "void". Your method should return HttpResponseMessage with the StatusCode I noted above.

Upvotes: 1

Related Questions