C# 4.0 WebAPI可以';t调用Put方法
我在服务器上有以下代码:C# 4.0 WebAPI可以';t调用Put方法,c#-4.0,asp.net-web-api,httpclient,C# 4.0,Asp.net Web Api,Httpclient,我在服务器上有以下代码: public class UploadController : ApiController { public void Put(string filename, string description) { ... } public void Put() { ... } 并尝试从客户端调用它: var clientDescr = new HttpClient();
public class UploadController : ApiController
{
public void Put(string filename, string description)
{
...
}
public void Put()
{
...
}
并尝试从客户端调用它:
var clientDescr = new HttpClient();
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("filename", "test"));
postData.Add(new KeyValuePair<string, string>("description", "100"));
HttpContent contentDescr = new FormUrlEncodedContent(postData);
clientDescr.PutAsync("http://localhost:8758/api/upload", contentDescr).ContinueWith(
(postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
var clientDescr=newhttpclient();
var postData=新列表();
添加(新的KeyValuePair(“文件名”、“测试”);
添加(新的KeyValuePair(“description”,“100”));
HttpContent contentDescr=新的FormUrlEncodedContent(postData);
clientDescr.PutAsync(“http://localhost:8758/api/upload,contentDescr)。是否继续(
(任务后)=>
{
postTask.Result.EnsureSuccessStatusCode();
});
但这段代码调用第二个put方法(不带参数)。为什么以及如何正确调用first put方法?这里有几个选项: 您可以选择在查询字符串中传递参数,只需将URI更改为:
http://localhost:8758/api/upload?filename=test&description=100
或者,您可以通过将操作更改为如下所示,让Web API为您解析表单数据:
public void Put(FormDataCollection formData)
{
string fileName = formData.Get("fileName");
string description = formData.Get("description");
}
您还可以选择创建一个具有fileName和description属性的类,并将其用作参数,Web API应该能够为您正确绑定它。谢谢。第二个问题-如何调用Delete方法?我尝试调用clientDel.DeleteAsync(“),但不调用public void Delete(int id)…这应该可以正常工作-可能请求甚至没有到达Web API