首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >一个WCF OperationContract方法的WebGet属性可以有多个ResponseFormat类型吗?

一个WCF OperationContract方法的WebGet属性可以有多个ResponseFormat类型吗?
EN

Stack Overflow用户
提问于 2009-11-17 13:49:46
回答 2查看 13.8K关注 0票数 17

我有一个描述WCF服务中使用的方法的ServiceContract。该方法有一个定义UriTemplate和ResponseFormat的WebGet属性。

我想重用单个方法,并拥有具有不同UriTemplates和不同ResponseFormats的多个WebGet属性。基本上,我希望避免使用多个方法来区分返回类型是XML还是JSON。不过,在我到目前为止看到的所有示例中,都需要为每个WebGet属性创建不同的方法。这是一个示例OperationContract

代码语言:javascript
复制
[ServiceContract]
public interface ICatalogService
{
    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)]
    Product GetProduct(string id);

    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)]
    Product GetJsonProduct(string id);
}

使用上面的例子,我想对xml和json返回类型使用GetProduct方法,如下所示:

代码语言:javascript
复制
[ServiceContract]
public interface ICatalogService
{
    [OperationContract]
    [WebGet(UriTemplate = "product/{id}/details?format=xml", ResponseFormat = WebMessageFormat.Xml)]
    [WebGet(UriTemplate = "product/{id}/details?format=json", ResponseFormat = WebMessageFormat.Json)]
    Product GetProduct(string id);
}

有没有办法做到这一点,这样我就不用编写不同的方法来返回不同的ResponseFormats了?

谢谢!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1746857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档