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

Viewed 494

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

2 Answers

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

enter image description here

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

Related