Unable to call RESTful web services methods
- by Alessandro
Hello, I'm trying to dive into the RESTful web services world and have started with the following template:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Test {
   // TODO: Implement the collection resource that will contain the SampleItem instances
   [WebGet(UriTemplate = ""), OperationContract]
   public List<SampleItem> GetCollection() {
     // TODO: Replace the current implementation to return a collection of SampleItem instances
     return new List<SampleItem>() {new SampleItem() {Id = 1, StringValue = "Hello"}};
   }
   [WebInvoke(UriTemplate = "", Method = "POST"), OperationContract]
   public SampleItem Create(SampleItem instance) {
     // TODO: Add the new instance of SampleItem to the collection
      throw new NotImplementedException();
   }
   [WebGet(UriTemplate = "{id}"), OperationContract]
   public SampleItem Get(string id) {
      // TODO: Return the instance of SampleItem with the given id
      throw new NotImplementedException();
   }
   [WebInvoke(UriTemplate = "{id}", Method = "PUT"), OperationContract]
   public SampleItem Update(string id, SampleItem instance) {
      return new SampleItem {
               Id = 99,
               StringValue = "Done"
             };
   }
   [WebInvoke(UriTemplate = "{id}", Method = "DELETE"), OperationContract]
   public void Delete(string id) {
      // TODO: Remove the instance of SampleItem with the given id from the collection
      throw new NotImplementedException();
   }
}
I am able to perform the GET operation but I am unable to perform PUT, POST or DELETE requests.
Can anyone explain me how to perform these operations and how to create the correct URLs?
Best regards
Alessandro