WCF REST: indicate content type in WebGet attribute doesn't seem to work

maybe I am doing something wrong, but I am returning XML from my WCF Rest service, which was built since VS 2010. In the fiddle, 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 to the webget attribute in my method, but it still returns text / html ... I suppose I should return the content type text / xml because I really return XML?

Here is my method, I added ResponseFormat to the attribute ... I was not sure that I needed bodystyle (I have no idea what it does, but saw it in the 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 changing and rebuilding 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

, , NO EFFECT, ...

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

fiddler, - /xml.

, , , .

- , ?

+5
3

Firefox /html, text/xml, WCF, xml json, "" , , .

,

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

. , , /.

+5

., ,

WCF ResponseFormat WebGet

, , ,

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

ResponseFormat - .

0

, MSDN WCF WebHttp Services .NET 4:

.NET.NET Framework, .

As soon as I did this and restarted the project, I was able to add System.ServiceModel.Web from the list of links.

I hope this helps someone.

0
source

All Articles