首页 > 解决方案 > ASP.NET Core 5,带有来自正文的参数的 HttpPut 请求始终返回 Http 400 错误请求

问题描述

首先,这是我在这个网站上的第一篇文章。这也是我尝试发布到 Web 服务器的第一个应用程序。所以如果有些事情不是那么清楚,或者不是他们应该的那样,请原谅我。

问题如下:当我向后端发送 PUT 请求时,总是收到“Http 400 Bad request”错误。当我将后端上传到 IIS 托管服务器时,出现了这个问题。当我在 localhost 上运行应用程序时,问题不会发生。

我创建了一个简单版本的 PUT 请求,如下所示:

  [HttpPut("{id:int}")]
  public ActionResult PutTest(int id, [FromBody]TestClass model)
  {
     return Ok(model.Prop1);          
  }

测试类如下:

namespace WebApi.Models.Accounts
{
   public class TestClass
   {
      public string Prop1 { get; set; }
      public string Prop2 { get; set; }
   }
}

例如,当我运行此代码时https://localhost:5000,它工作正常。但是当我将后端部署到我的域时,假设https://www.mySite.app它总是返回一个错误的请求错误。

我已经尝试进一步简化代码。并注意到,当我只在下面的 PUT 操作中要求一个单一参数时,它工作正常。

  [HttpPut("{id:int}")]
  public ActionResult PutTest(int id)
  {
     return Ok(id);          
  }

web.config我使用的文件非常简单,如下所示:

<?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=".\WebApi.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="InProcess">
            <handlerSettings>
               <handlerSetting name="debugLevel" value="file" />
               <handlerSetting name="debugFile" value=".\logs\aspnetcore-debug.log"/>
            </handlerSettings>
         </aspNetCore>
      </system.webServer>
   </location>
</configuration>

我正在用 Postman 测试我的后端。我能够成功地向后端中的所有其他 Http 函数发送请求,除了这个在正文中带有参数的 PUT 请求。

我被困在这几天了,似乎找不到解决方案。所以任何帮助将不胜感激。顺便说一句,所有其他 Http 类都可以正常工作,POST、GET、DELETE 等等。

标签: c#httpasp.net-coreiis

解决方案


您是否尝试过使用 Fiddler 或其他东西在失败时检查原始请求/响应?除了上述 S. 10 Brinke 的建议之外,这也将有所帮助。

这些来自 Microsoft 的页面还可以帮助您排除故障并捕获 IIS 日志 https://docs.microsoft.com/en-us/iis/troubleshoot/diagnosing-http-errors/troubleshooting-http-400-errors-in-iis https ://docs.microsoft.com/en-US/troubleshoot/aspnet/error-logging-http-apis


推荐阅读