简体   繁体   中英

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

First of all, this is my first post on this website. And also this is my first application that I am trying to publish onto a web server. So forgive me if some things aren't that clear, or not as they are supposed to.

The problem is as follows: when I send a PUT request to my backend, I always get a "Http 400 Bad request" error. This problem showed up when I uploaded the backend to an IIS hosting server. When I run the application on localhost, the problem doesn't occur.

I've created a simple version of a PUT request, that is as follows:

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

The TestClass is as follows:

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

When I run this code on https://localhost:5000 for instance, it works fine. But when I deploy the backend to my domain, let's say https://www.mySite.app , it always returns a bad request error.

I've tried simplifying the code even further. And noticed that when I only ask for a singe parameter in the PUT action like below, it works fine.

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

The web.config file that I am using is quite simple, it looks like this:

<?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>

I'm testing my backend with Postman. I am able to successfully send requests to all other Http functions in my backend, except for this PUT request with parameters in the body.

I'm stuck at this for a few days now, and can't seem to find the solution. So any help would be appreciated. All other Http classes work fine btw, POST, GET, DELETE and so on..

Have you tried using Fiddler or something to check the raw request/response when it fails? That would also help on top of what is suggested by S. ten Brinke above.

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

I used your code to test but no problem was found. I suggest you use FRT to see which module has the problem.

在此处输入图像描述

You can use FRT to catch 400 error and then view detailed logs, for more information about FRT, you can refer to this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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