WCF Restful服务GET/POST

ASR*_*ASR 8 c# rest wcf

我可以这样做吗?

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

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

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

GSe*_*rjo 22

是的,你可以,这里来自设计和构建RESTful Web服务指南

[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) {...}
    ...
}
Run Code Online (Sandbox Code Playgroud)

至于我,这种设计RESTful Web服务很糟糕.这个ServiceContrcat是:

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

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

您可以在此处找到详细说明:使用WCF构建基于RESTful消息的Web服务,此处的代码示例:Nelibur和Nelibur nuget package here