Dynamically set ResponseFormat of Web Service WebMethod at runtime in ASP.Net

born2achie
 
on Dec 24, 2013 09:54 AM
4463 Views

Hi,

I am using webservice in my project and i have tricky scenario to produce the ResponseFormat(json and XML).

 

I have a scenario to to produce the webservice output as json and xml. have to provide this fesibility to the consumer who is consuming the webservice.

for json : 

 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

for xml : 

 [ScriptMethod(ResponseFormat = ResponseFormat.xml)]

 

how can i provide the feasibility to the consumenr to choose which responseformat they want. i can write two methods one is for xml and another noe is for Json. Is it possible to write one method and any custom paramater can be set to choose which reposneformat they want.

is there any way to pass the responseformat to webservice to choose which one ot produce the output.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Dec 25, 2013 02:54 AM

You need to create two methods one for XML and one for JSON

 

born2achie
 
on Dec 27, 2013 08:30 AM

Hi Muddassar,

 

How about this, can we achieve something like this. 

 

[WebMethod]
        public string GetSomething(string responseType)
        {
            string result = "";

            if (responseType == "json") 
            { 
               //code convert object to json string for result
            }
            else if (responseType == "xml") 
            {
                //code convert object to xml string for result
            }
             
            return result;

        }