Reputation: 116840
When debugging a web service, I can test the functions using the default WSDL interface that is provided that lets me input some values for the parameters. This is very handy but outputs only XML. Is it possible to enable more options at this stage? (JSON, CSV)
Or if that is not possible, I would like to add an extra parameter to the API call, filetype=[json,csv]
but how would I write this back in that format? Do I pass it as a string?
Upvotes: 2
Views: 299
Reputation: 10390
I would suggest using ASP.NET MVC 3 and create an action that returns a JsonResult. This Action can execute your WebMethod and serialize your results as JSON. (this is for JSON only).
For more flexibility you can use an ASP.NET (Web Forms) Generic Handler which gives you a lot of control over response type and content.
You can also consider the Web API capabilities in ASP.NET MVC 4. It enables a broad set of request and response formats.
This stack overflow thread touches on JsonResult vs. Web API: MVC4 Web API or MVC3 JsonResult
Upvotes: 1
Reputation: 9044
I am assuming you are using WCF. There are a couple of simple ways you can choose between XML or JSON results. One is to have different endpoints, and the other is to have different methods. The second option caters for your request to include a parameter on the API call, but I will briefly describe both. Consider the endpoints below:
<endpoint address="/rest/" behaviorConfiguration="web" binding="webHttpBinding" contract="WebApplication1.Interface.ITestRest" />
<endpoint address="/json/" behaviorConfiguration="web" binding="webHttpBinding" contract="WebApplication1.Interface.ITestJson" />
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WebApplication1.Interface.ITestBoth" />
The first two relate to option 1 to differentiate by endpoint (either /rest/ or /json/ will be in the url before the method, and both interfaces can define the same signature so it can just be implemented once). The last one relates to option 2 to have two methods on the interface. Here is a sample set of interfaces for the above:
[ServiceContract]
public interface ITestJson
{
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/Echo/{Text}",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Echo(string Text);
}
[ServiceContract]
public interface ITestRest
{
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/Echo/{Text}",
RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string Echo(string Text);
}
[ServiceContract]
public interface ITestBoth
{
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/Echo?Text={Text}&Format=json",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string EchoJ(string Text);
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/Echo?Text={Text}&Format=xml",
RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
string EchoR(string Text);
}
Then a class that implements this:
public class Signature : ITestJson, ITestRest, ITestBoth
{
public string Echo(string Text)
{
return Text;
}
public string EchoR(string Text)
{
return Text;
}
public string EchoJ(string Text)
{
return Text;
}
Now you can use this in the following ways:
Service1.svc/json/echo/xxx
Service1.svc/rest/echo/xxx
Service1.svc/echo?Text=xxx&Format=json
Service1.svc/echo?Text=xxx&Format=rest
As I said at the start, these are a couple of simple ways to choose XML or Json. Your request asked for CSV as well. Currently there is no simple way to return CSV. I did find this project on CodePlex that can return TXT, but I have not checked it out.
Upvotes: 2