Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF REST: specify content-type on WebGet Attribute doesn't seem to be working

probably something i doing wrong, but i am returning XML from my WCF Rest service which is built with VS 2010. In fiddler you can see here that it returns test/html as the content-type

 HTTP/1.1 200 OK
 Cache-Control: private
 Content-Length: 222
 Content-Type: text/html; charset=utf-8
 Server: Microsoft-IIS/7.5
 X-AspNet-Version: 4.0.30319
 X-Powered-By: ASP.NET
 Date: Mon, 16 Aug 2010 20:49:55 GMT

So i went ahead and added the following on the webget attribute on my method but it still returns text/html ... I presume that i should return the content type of text/xml because i am in fact returning XML?

Heres my method, i added the ResponseFormat to the attribute... I wasn't sure if i needed bodystyle (i have no idea what it does but saw it in an example :-) )

    [WebGet(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
    public List<SampleItem> GetCollection()
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
    }

anyway after the change and rebuilding of the project it still returns the wrong content type ... am i missign somthing?

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 222
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 16 Aug 2010 20:54:15 GMT

EDIT

Ok i got a working solution but the attribute method has NO EFFECT, very strange...but if i put this

  WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

Now i check fiddler and the content-type is actually text/xml.

But i need to put this in every method and the attribute method seems to have no effect.

Anybody know why?

like image 576
mark smith Avatar asked Aug 16 '10 20:08

mark smith


1 Answers

According to this the Firefox request headers has a higher priority for text/html than text/xml, resulting in WCF service methods decorated with xml or json returning with the "wrong" response, although I can imagine it is the correct behavior.

You can force a response content type by explicitly setting

WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

or equivalent. I guess this is the only alternative if you truly want to force a specific content type response for all browsers/clients.

like image 53
angularsen Avatar answered Nov 13 '22 21:11

angularsen