Veeru
Veeru

Reputation: 463

How to change wcf response type based on parameter?

my WCF service get request looks like below:

  1. ../myservice.svc/search?q=keywords&op=xml
  2. ../myservice.svc/search?q=keywords&op=json

so based on param op, response type should change. How to accomplish this?

Upvotes: 1

Views: 1160

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

Here you have complex article about setting response in different formats. If you are using WCF 4 I would definitely think about setting automaticFormatSelectionEnabled="true" in WebHttpBehavior. It will automatically choose response format (XML or JSON) based on client's HTTP Accept header.

Upvotes: 2

Jonathan
Jonathan

Reputation: 6003

One way would be the WebGetAttribute : http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx

public class MyService
{
    [OperationContract]
    [WebGet(UriTemplate = "search?q={keyword}&op=xml")]
    string GetXml(string keyword);

    [OperationContract]
    [WebGet(UriTemplate = "search?q={keyword}&op=json")]
    string GetJson(string keyword);
}

note: above not tested

Upvotes: 2

Related Questions