我收到了很多关于休息服务中的 post 和 get 方法的文章,但我没有得到任何关于 put/delete 的好文章。我创建了一项休息服务并尝试调用四项操作。获取并发布工作,但不放置/删除。我将在这里提供我的代码;
在 silverlight put 和 post 方法中调用 rest 服务
const string uridel =
"http://localhost:50211/CustomerService.svc/deletecustomer/1";
const string uriput =
"http://localhost:50211/CustomerService.svc/modifycustomer/1";
client.DownloadStringCompleted += (s, ev) =>//delete method
{
XDocument xml = XDocument.Parse(ev.Result);
var Customer = from results in xml.Descendants
("CustomerResponse")
select new CustomerResponse
{
CustomerId = Int32.Parse(results.Descendants
("CustomerId").First().Value),
};
int id = Customer.Select(w => w.CustomerId).FirstOrDefault();
MessageBox.Show("result is :" + id);
};
client.DownloadStringAsync(new Uri(uridel), "DELETE");
CustomerResponse cusres = new CustomerResponse();//put method
cusres.CustomerName = textBox1.Text;
cusres.CustomerPh = textBox2.Text;
DataContractSerializer dataContractSerializer =
new DataContractSerializer(typeof(CustomerResponse));
MemoryStream memoryStream = new MemoryStream();
dataContractSerializer.WriteObject(memoryStream, cusres);
string xmlData = Encoding.UTF8.GetString(memoryStream.ToArray(), 0,
(int)memoryStream.Length);
client.UploadStringCompleted += (s, ev) =>
{
XDocument xml = XDocument.Parse(ev.Result);
var Customer = from results in xml.Descendants("CustomerResponse")
select new CustomerResponse
{
CustomerId = Int32.Parse(results.Descendants
("CustomerId").First().Value),
};
int id = Customer.Select(w => w.CustomerId).FirstOrDefault();
MessageBox.Show("result is :" + id);
textBox1.Text = "";
textBox2.Text = "";
};
client.Headers[HttpRequestHeader.ContentType] = "application/xml";
client.UploadStringAsync(new Uri(uriput), "PUT", xmlData);
在 wcf 服务中
[OperationContract]
[WebInvoke(Method = "DELETE",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "deletecustomer/{id}")]
CustomerResponse DeleteCustomer(string id);
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "modifycustomer/{id}")]
CustomerResponse ModifyCustomer(string id,CustomerResponse cusres);
对于删除,我遇到了一个异常,即找不到服务器,而对于 put 方法,我遇到了错误,例如此请求不支持指定的方法。任何人都可以建议错误在哪里..或者建议可以在silverlight中使用put和delete方法的好文章?