Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResponseFormat for WCF in JSON and XML at the same time?

Tags:

c#

wcf

When requesting an object for example using REST, is it possible to get the response in json and xml format or do I have to create UriTemplates that are something like:

[WebInvoke(UriTemplate="&format=json?user/{id}", ResponseFormat=WebMessageFormat.Json)]

[WebInvoke(UriTemplate="&format=xml?user/{id}", ResponseFormat=WebMessageFormat.Xml)]

The reason I ask is because I may need one format returned for an app on an android phone for example and another type returned for an app on a laptop.

Also, Can the methods have the same name such as Register or do I have to have one called:

RegisterJSON(User user) and another called RegisterXML(User user)

like image 553
Xaisoft Avatar asked Jan 22 '23 09:01

Xaisoft


1 Answers

It is available out of the box in WCF 4 (.NET 4.0). Check AutomaticFormatSelectionEnabled property of WebHttpBehavior. You can also set this property from configuration. I showed the example here.

like image 142
Ladislav Mrnka Avatar answered Feb 01 '23 00:02

Ladislav Mrnka