8

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

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
ASR
  • 429
  • 3
  • 6
  • 16
  • Is there a requirement that you must use WCF? – Petar Vučetin Jul 30 '12 at 17:09
  • Take a look at http://stackoverflow.com/questions/5431669/rest-wcf-service-consume-querystring-parameters – Mike Perrenoud Jul 30 '12 at 17:15
  • yeah must use WCF. @MIke the example isn't exactly what I am trying to do, it uses only query string. I am trying to POST with a query string? – ASR Jul 30 '12 at 17:20
  • 1
    Ok, take a look at this one. They have a posted body and receive query parameters. http://stackoverflow.com/questions/11261119/wcf-post-with-query-string – Mike Perrenoud Jul 30 '12 at 17:30
  • Thnx thats what I thought at first that either access query string or post from code, but wanted a second opinion. Tx for your help. – ASR Jul 30 '12 at 17:51

1 Answers1

25

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

GSerjo
  • 4,725
  • 1
  • 36
  • 55
  • 1
    Yah, I'm gonna necro this post; it's worth pointing something out.I read the CodeProject article linked in this answer, you're doing some really cool stuff in code with creating a super-generic, flexiable interface.However, you are not creating a RESTful interface.There are specific principles around what makes an API RESTful and the IJsonService is without a doubt not a RESTful service definition.Nothing says you can't keep using that style of API creation, but you can't call it RESTFul. Very cool code though.... – TSmith Apr 23 '20 at 16:07
  • thank you :) as for me it's a paradigm, not an implementation. Is wheel a car? No. but with other wheels and etc it's a car.. the same for IJsonService. Restful service it's a paradigm, how a service should behave not implementation. https://en.wikipedia.org/wiki/Representational_state_transfer – GSerjo Apr 24 '20 at 17:29