0

我在服务器上有以下代码:

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

但是这段代码调用了第二个 put 方法(不带参数)。为什么以及如何正确调用 first put 方法?

4

1 回答 1

1

您在这里有几个选择:

您可以选择传递查询字符串中的参数,只需将 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 应该能够为您正确绑定它。

于 2013-06-22T21:13:55.747 回答