WebAPI文件上传功能,带有从c#desktop app调用的参数

时间:2017-03-28 21:19:14

标签: c# asp.net-web-api

我正在开发一个调用WebAPI的c#桌面应用程序。一个函数上传文件并具有参数。

我找到了使用MultipartFormDataContent的示例,但如果调用中有参数,我就无法使用它。

如何设置PostAsync以在内容中同时包含MultiFormDataContent和url参数,而不将它们作为requestURI的一部分嵌入?

API函数

    //Can Call this
    [HttpPost]
    [Route("UploadFileTest")]
    public async Task<HttpResponseMessage> UploadFileTest()
    {...

    //How to do this
    [HttpPost]
    [Route("UploadFileTest")]
    public async Task<HttpResponseMessage> UploadFileTest(string toolToken)
    {...

C#调用它

    //this works with the first API call
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("www.someURL/APICalls/");
    var content = new MultipartFormDataContent();
    content.Add(new StreamContent(fileStream), "\"file\"", string.Format("\"{0}\"", fileInfo.Name));
    //adding the following line doesn't work with the second API function, it can't find the API function to call.
    //content.Add(new StringContent("12345"), "toolToken");

    Task taskUpload = client.PostAsync("UploadFileTest", content).ContinueWith(task =>
    {...

1 个答案:

答案 0 :(得分:0)

尝试[FromBody]

[HttpPost]
[Route("UploadFileTest")]
public async Task<HttpResponseMessage> UploadFileTest([FromBody]string toolToken)
{...