我收到了很多关于休息服务中的 post 和 get 方法的文章,但我没有找到任何关于 put/delete 的好文章。我创建了一个休息服务并尝试调用四个操作。获取并发布工作但不放置/删除。我将在这里提供我的代码;
在silverlight中调用rest服务的put和post方法
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 方法,我得到一个错误,比如指定的方法在这个请求中不受支持。任何人都可以建议错误出在哪里..或建议可以使用 put 和 delete 方法在 silverlight 中使用的好文章吗?
最佳答案
第 1 步: 在 Silverlight 应用程序检查中,您已在 App() 构造函数的 App.xaml.cs 上添加了以下行。
HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
HttpWebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
第 2 步:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*" http-methods="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
将 xml 保存为服务宿主项目下的“clientaccesspolicy.xml”。
关于c# - 如何调用 delete/put 方法以从 silverlight 中恢复服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15068000/