8

我可以做这样的事情吗?

[OperationContract]    
[WebInvoke
  (  
    Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "/abc{integerParam}"
  )
]
ResultStruct abc( int integerParam, CustomClass secondParam );

这里的想法是我可以在 url 中传递第一个参数(整数),但 secondParam 来自 POST。这甚至可能吗?

我从 WCF REST 开始,不确定如何分配参数。任何指示都会有所帮助,谢谢

4

1 回答 1

24

是的,你可以,这里来自A Guide to Designing and Building RESTful Web Services

[ServiceContract]
public partial class BookmarkService
{
    [WebInvoke(Method = "PUT", UriTemplate = "users/{username}")]
    [OperationContract]
    void PutUserAccount(string username, User user) {...}

    [WebInvoke(Method = "DELETE", UriTemplate = "users/{username}")]
    [OperationContract]
    void DeleteUserAccount(string username) {...}

    [WebInvoke(Method = "POST", UriTemplate = "users/{username}/bookmarks")]
    [OperationContract]
    void PostBookmark(string username, Bookmark newValue) {...}

    [WebInvoke(Method = "PUT", UriTemplate = "users/{username}/bookmarks/{id")]
    [OperationContract]
    void PutBookmark(string username, string id, Bookmark bm) {...}

    [WebInvoke(Method = "DELETE", UriTemplate = "users/{username}/bookmarks/{id}")]
    [OperationContract]
    void DeleteBookmark(string username, string id) {...}
    ...
}

对我来说,这种设计 RESTful Web 服务的方式很糟糕。这个 ServiceContrcat 是:

  • 不可维护,脆弱的远程接口
  • 必须创建太多方法
  • 多态性不存在

我相信,远程接口应该是稳定灵活的,我们可以使用基于消息的方法来设计 Web 服务。

你可以在这里找到详细的解释:Building RESTful Message Based Web Services with WCF,代码示例在这里:Nelibur and Nelibur nuget package here

于 2012-07-30T18:49:46.793 回答