views:

130

answers:

1

Hi there,

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?

A: 

See e.g.

http://stackoverflow.com/questions/992533/wcf-responseformat-for-webget/1016266#1016266

I think you want e.g.

OutgoingWebResponseContext context = 
    WebOperationContext.Current.OutgoingResponse; 
context.ContentType = "image/jpeg"; 

ResponseFormat controls something else.

Brian
Hi Brian, thanks ... well i have been reading this and unless i am mistaken... this is for when you want to return CUSTOM formats that are not supported but the xml format is supported ..... see here http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspxSo i assume it should work but its not
mark smith