Azure ASP.NET Core web api returns 404 代理 multipart/form-data 请求

Azure ASP.NET Core web api returns 404 for proxied multipart/form-data request

我是 Azure 的新手,正在尝试设置我的 nextjs 客户端应用程序和我的 ASP.NET 核心后端应用程序。现在一切似乎都很好,除了文件上传。它在本地主机上工作,但在生产中后端 returns 一个 404 网页(附图)在到达实际的 API 端点之前。我还成功测试了从我的计算机在 Postman 中发出 multipart/form-data POST 请求。

我实现这个的方式是我通过 api 路由(客户端的服务器端)代理从浏览器上传到后端。我必须通过客户端服务器端从 httpOnly cookie 附加 Bearer 令牌。

我在 Startup.cs 中启用了 CORS:

app.UseCors(builder => { builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); });

前端和后端应用程序在个人服务中 运行 我也尝试在 Azure 门户中启用 CORS,但我只能允许来源,而不是 headers 和方法?错误消息并不表示 CORS 问题,但我只是想确保..

据我所知,请求看起来不错,具有正确的 URL 和来源。我怀疑我在天蓝色中遗漏了一些配置,但我没有按照错误消息中的提示进行进一步操作。

有什么可能导致这种情况的建议吗?或者我可以从哪里开始寻找。我不太确定在哪里可以找到此问题的日志输出。

  • Cross-Origin 资源共享 (CORS) 允许外部主机上浏览器中的 JavaScript 代码 运行 与后端交互。

  • 要允许全部,请使用“*”并从列表中删除所有其他来源。

I could only allow origins, not headers and methods?

在您的 web.config 文件中添加以下配置以允许 headers 和方法。

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add  name="Access-Control-Allow-Origin"  value="*"  />
      <add  name="Access-Control-Allow-Headers"  value="Content-Type"  />
      <add  name="Access-Control-Allow-Methods"  value="GET, POST, PUT, DELETE, OPTIONS"  />
    </customHeaders>
  </httpProtocol>
</system.webServer>

在 web.config=> =>handlers 标签中添加以下代码段

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\yourURI.dll" stdoutLogEnabled="false" stdoutLogFile="\?\%home%\LogFiles\stdout" hostingModel="InProcess" />
        </system.webServer>
    </location>
</configuration>
  • 确保路由正确完成

[Route("api/[controller]")] 应该在你的控制器中 class.

client cache is still pointing to the domain to ols ip address. clear the cache by running the command ipconfig/flushdns

更多信息请参考Custom domain has not been configured inside Azure ,Azure WebApps Custom Domain 404 error - Flush DNS and SO Thread

我终于搞定了。我认为代理 http 请求中的主机 header 没有改变。我只更改了代理请求的 URL,但我也通过手动设置主机解决了这个问题。这也解释了为什么它在本地主机上工作,因为客户端和后端都在同一主机上 运行。