Search code examples
asp.net-coreasp.net-web-apiiis-express

PUT, DELETE Not Working (error 404) for ASP.NET core Web API Templates


I have tried several modifications even in .config file to enable those verbs but nothing works. GET method works fine but 404 for PUT and DELETE.

I already tried removing WebDav Module and other IIS related modifications.

Here is my Controller :

[HttpPut("{id:length(24)}")]
public IActionResult Update(string id, Client clientIn)
{
    var client = _clientService.Get(id);

    if (client == null)
    {
        return NotFound();
    }

    _clientService.Update(id, clientIn);

    return NoContent();
}

[HttpDelete("{id:length(24)}")]
[Route("api/Clients/{id}")]
public IActionResult Delete(string id)
{
    var client = _clientService.Get(id);

    if (client == null)
    {
        return NotFound();
    }

    _clientService.Remove(client.ClientId);

    return NoContent();
}

Solution

  • I do not know how you set the HttpGet. In this code, because you limit the length of the id, the id must be up to 24 characters to be mapped. You can change it like this.

        [HttpPut("{id:maxlength(24)}")]
        public IActionResult Update(string id, Client clientIn)
        {
            //
        }
    

    And you can refer to this route constraints.