繁体   English   中英

ASP.NET 核心 5,带有来自正文的参数的 HttpPut 请求始终返回 Http 400 错误请求

[英]ASP.NET Core 5, HttpPut request with parameters from body always returns Http 400 Bad request

首先,这是我在这个网站上的第一篇文章。 这也是我尝试发布到 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 等等。

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

These pages from Microsoft would also help you troubleshoot and capture the IIS logs 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

我用你的代码测试,但没有发现问题。 建议你用FRT看看是哪个模块有问题。

在此处输入图像描述

您可以使用 FRT 捕获 400 错误,然后查看详细日志,有关 FRT 的更多信息,您可以参考此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM