2
我得到了很多關於post的文章,並在休息服務中獲取方法,但我沒有得到任何好的put/delete方法。我創建了一個休息服務並試圖調用四個操作。獲取和發佈工作,但不是放入/刪除。我會在這裏提供我的代碼;如何調用刪除/放置方法從silverlight中休息服務
在WCF服務的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);
[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方法我的錯誤得到了像此請求不支持指定的方法。任何人都可以提出錯誤的地方..或建議好的文章,可以使用put和delete方法在silverlight中消耗?