简体   繁体   中英

WCF Restful service GET/POST

Can I do something like this?

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

Idea here being that I can pass first parameter( integer ) in the url, but secondParam comes from POST. Is this even possible?

I started with WCF REST and not sure about how parameters are assigned. Any pointers will be helpful thank you

Yes you can, here is from 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) {...}
    ...
}

As for me, this kind of designing RESTful web services is terrible. This ServiceContrcat is:

  • unmaintainable , brittle remote interface
  • Have to create too many methods
  • Polymorphism is absent

I believe, that remote interface should be stable and flexible , we can use message based approach for designing web services.

You can find detailed explanation here: Building RESTful Message Based Web Services with WCF , code samples here: Nelibur and Nelibur nuget package here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM