我怎样才能使除了默认的XML更多的输出格式? [英] How can I enable more output formats in addition to the default XML?

查看:161
本文介绍了我怎样才能使除了默认的XML更多的输出格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试一个Web服务,我可以测试使用提供的缺省WSDL接口,让我输入的参数值的一些功能。这是很方便,但只输出XML。是否有可能使在这个阶段更多的选择? (JSON,CSV)

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)

或者,如果这是不可能的,我想一个额外的参数添加到API调用,文件类型= [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?

推荐答案

我假设你正在使用WCF。有几个简单的方法可以XML或JSON结果之间选择。一个是具有不同的端点,而另一个是有不同的方法。第二个选项迎合您的要求包括API调用的参数,但我将简要介绍这两个。考虑下面的端点:

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" />

前两个涉及到选择1通过端点来区分(无论/ REST /或/ JSON /会在方法之前的URL,且这两个接口可以定义相同的签名,因此可以只实施一次)。最后一个涉及选项2,有接口上的两个方法。这里是对上述样品组的接口:

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

正如我在开始时说,这些都是一对夫妇的简单的方式来选择XML或JSON。您的要求要求CSV以及。目前还没有简单的方法来恢复CSV。我发现上codePLEX这个项目可以返回TXT,但我没有检查它出去。

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.

这篇关于我怎样才能使除了默认的XML更多的输出格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆