【问题标题】:StatusCode: 404, Response when calling the Web APIStatusCode: 404, 调用 Web API 时的响应
【发布时间】:2022-01-20 01:27:05
【问题描述】:

我有一个将文件内容上传到服务器的 Web API。

[HttpPost]
[Route("SaveFileContent")]
public async Task<FileResponse> SaveFileContent([FromForm] SaveFileContentRequest request)
{
     return await _service.SaveFile(request);
}

这是我对 API 的调用:

public async Task<FileResponse> SaveFileContent(SaveFileContentRequest request)
{
    try
    {
        var uri = "https://www.mycompanyurl.com";
        
        using (var client = new HttpClient())
        {
            using (var form = new MultipartFormDataContent())
            {
                 using (var fileContent = new ByteArrayContent(request.File))
                 {
                     fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");

                     form.Add(fileContent, "file", request.FileName);
                     form.Add(new StringContent(request.MemberId), "MemberId);
                     form.Add(new StringContent(request.Country), "Country);

                     client.BaseAddress = new Uri(uri);
                     HttpResponseMessage response = await client.PostAsync("/api/Document/SaveFileContent", form);
                     FileResponse result = JsonConvert.DeserializeObject<FileResponse>(response.Content.ReadAsStringAsync().Result);
                     return result;
                 }
             }
          }
       }
    }

我在 PostAsync() 收到此响应:

   {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
   {
        Date: Sun, 21 Apr 2013 12:00:03 GMT
        Server: Microsoft-HTTPAPI/2.0
        Content-Length: 165
        Content-Type: application/json; charset=utf-8
   }}

当我尝试在本地运行 API - 并使用 localhost uri - var uri = "http://localhost:51515";

它工作正常并得到 200 OK 响应。

【问题讨论】:

  • 如果在本地使用 HTTPS 会发生什么?

标签: c# asp.net http asp.net-web-api multipartform-data


【解决方案1】:

尝试使用完整路线

[Route("~/api/Document/SaveFileContent")]
public async Task<FileResponse> SaveFileContent([FromForm] SaveFileContentRequest request)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多