title: Write a webservice method with both URI and DataContract class parameters
body: <p>How can I get a web service method to access parameters in the URI and complex POST data?</p>
  
  <p>I have a web service method with some parameters in the URI:</p>
  
  <pre><code>"/api/{something}/{id}"
  </code></pre>
  
  <p>I also have a class marked with DataContract/Member attributes that I want the webservice method to accept. Note that my class contains a subclass also marked with DataContract/Member attributes.</p>
  
  <pre><code>[DataContract]
  public class MoreData{
      [DataMember]
      public string SomeString { get; set; }
  
      [DataMember]
      public SubClass SubData {get; set;}
  }
  </code></pre>
  
  <p>My method declaration looks like:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke( Method = "POST", UriTemplate = "/api/{something}/{id}")]
  public MyWebSvcRetObj Update(string something, string id)
  </code></pre>
  
  <p>How would I get my Update method to also accept the MoreData class? I've been able to write methods like:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke( Method = "POST")]
  public MyWebSvcRetObj Update(MoreData IncomingData)
  </code></pre>
  
  <p>but only if the URI did not specify any parameters, and the URI would look like</p>
  
  <pre><code>"api/Update"
  </code></pre>
  
  <p>Ultimately I want my users to be able to something like:</p>
  
  <pre><code>$.ajax({
      url: 'localhost/site/api/things/12345
      type: 'POST'
      data: {
          SomeString: 'I am a string'
          SubData: {
             //...more data
          }
      }
  });
  </code></pre>
  
  <p>I have tried putting a Stream as the final parameter in the Update method, but that doesn't seem to work for classes that have subclasses like the example I gave. It ends up having key/value pairs like "SubData[SomeSubDataProp]" : "Val". That seems wrong to me. I've also tried adding a MoreData parameter as the final param in the Update method, but I get an error that says it was expecting a stream (because it's expecting raw data). Is what I am trying to do possible? Or perhaps I am going about this the wrong way?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As Faizan Mubasher wrote, my users will be able to send post data if my web method looks like:</p>
  
  <pre><code>[OperationContract]
      [WebInvoke( Method = "POST", 
          UriTemplate = "/v1/{something}/{id}",
          RequestFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare
      )]
      [Description( "Update " )]
      public void Update( MoreData Incoming, string something, string id )
  </code></pre>
  
  <p>With the client looking like:</p>
  
  <pre><code>$.ajax({
      type: 'POST',
      url: 'http://localhost/app/api/something/12345',
      contentType: 'application/json',                  //need to specify type
      data: JSON.stringify({                            //note the "stringify" call
          SomeString: 'hello, world',
          SubClass: {
              SubStr: 'hello, again',
              SubInt: 98765
          }
      }
  });
  </code></pre>