无法调用RESTful Web服务方法

时间:2010-06-17 14:53:35

标签: c# web-services rest

我正在尝试深入了解RESTful Web服务世界并开始使用以下模板:

[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();
   }
}

我能够执行GET操作,但我无法执行PUT,POST或DELETE请求。

有人可以解释一下如何执行这些操作以及如何创建正确的URL吗?

祝你好运

的Alessandro

2 个答案:

答案 0 :(得分:0)

据我所知,WebInvoke目前仅支持GET和POST。我使用POST来执行PUT和DELETE操作。

答案 1 :(得分:0)

编辑 - 根据您的回答更新:

网址为“http://localhost/test/Test.svc/MethodName” postData是您要作为参数传递的数据。

在您的情况下,看起来您正在尝试传递类型。请记住,这是在URL中发布的。将类型的值分解为参数。

示例:“http://localhost/test/Test.svc/Create?id=123456&stringValue=newSampleItem

您需要将Operation Contract更改为接受int和字符串而不是SampleItem。

    [WebInvoke(UriTemplate = "Create?id={x}&stringValue={y}", Method = "POST"), OperationContract]
    public SampleItem Create(int id, string stringValue)
    {
       // Create and return the Sample Item. 
    }

让我知道它是怎么回事。

帕特里克。

嗨Alex,这是我用来发布Restful服务的内容......

// Create the request
WebRequest request; 
request = WebRequest.Create(url + postData); 
request.Method = "POST";  

byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
request.ContentType = "application/x-www-form-urlencoded"; 
request.ContentLength = byteArray.Length;

// Get the request stream. 
Stream dataStream = request.GetRequestStream(); 

// Write the data to the request stream. 
dataStream.Write(byteArray, 0, byteArray.Length); 

// Close the Stream object. 
dataStream.Close();

// Process the response
Stream responseStream; 
responseStream = request.GetResponse().GetResponseStream();

StreamReader objReader = new StreamReader(responseStream); 

StringBuilder sb = new StringBuilder(); 
string sLine = ""; 
int i = 0; 

while (sLine != null) 
{
    i++;
    sLine = objReader.ReadLine();
    sb.Append(sLine);
}

responseStream.Close();


string responseXML = sb.ToString()

祝你好运,

帕特里克