¿Cómo puedo habilitar más formatos de salida además del XML predeterminado?

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?

preguntado el 04 de julio de 12 a las 07:07

2 Respuestas

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 sencillo 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 este project on CodePlex that can return TXT, but I have not checked it out.

Respondido 05 Jul 12, 00:07

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: API web de MVC4 o JsonResult de MVC3

contestado el 23 de mayo de 17 a las 12:05

+1 Thank you for the answer. Do you have any pointers on how to create the capabilities provided by WCF using Web API? I am particularly interested in the automatic interface that it creates that lets me play with the API. Is there a parallel to this in MVC4? - Leyenda

... as well as instructive videos - glenn ferrie

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.