Home > OS >  How can I have my HTTP PUT routes treated the same way as PATCH routes?
How can I have my HTTP PUT routes treated the same way as PATCH routes?

Time:02-14

I have an ASP.NET Core web API controller base class which can be shortened to the following:

[Route("my/route")]
public abstract class ControllerBase
{
    [HttpPut]
    [Route("")]
    public MyObject Update([FromBody] MyObject obj) => obj;
    
    [HttpPut]
    [Route("{*id}", Order = 1)]
    public MyObject Update([FromUri] String id, [FromBody] MyObject obj) => obj;
    
    [HttpPatch]
    [Route("")]
    public MyObject Patch([FromBody] JObject data) => new MyObject();
    
    [HttpPatch]
    [Route("{*id}", Order = 1)]
    public MyObject Patch([FromUri] String id, [FromBody] JObject data) => new MyObject();
}

Now, imagine calls to the following four endpoints:

  1. PUT /my/route
  2. PUT /my/route/42
  3. PATCH /my/route
  4. PATCH /my/route/42

In ASP.NET (using .NET Framework), all four used to work.

Now that the application has been switched to .NET Core and uses ASP.NET Core, calls 1 and 2, i.e. the two HTTP PUT calls, return 405/Method Not Allowed. The HTTP PATCH calls still work as before.

It seems ASP.NET Core's routing behaves somewhat differently from that of ASP.NET, but why would it behave the same way for one HTTP method and differently for another?

After doing some digging, I found the answers to HTTP Verbs PUT and DELETE: 405 Method not allowed - how to allow?, where it was suggested that in ASP.NET Core, the query string parameters need to be added to the method attribute, as well. Therefore, I've tried changing my PUT methods as follows, but to no avail:

    [HttpPut("")]
    [Route("")]
    public MyObject Update([FromBody] MyObject obj) => obj;
    
    [HttpPut("{*id}", Order = 1]
    [Route("{*id}", Order = 1)]
    public MyObject Update([FromUri] String id, [FromBody] MyObject obj) => obj;

What do I have to do to make the PUT endpoints work again, the same as the PATCH endpoints still do?

CodePudding user response:

The problem was the WebDAV module in IIS.

I used the Add/Remove Features function of Windows to remove it completely from my system, and once that was done, HTTP PUT calls would work again as expected.

  • Related