WebAPI 文件上传函数,其中包含从 c# 桌面应用调用的参数



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

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

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

接口函数

    //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 =>
    {...

尝试[FromBody]

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

最新更新