ASP.NET - How to make a controller endpoint for a HTTP Delete request

Like with HttpGet HttpPut or HttpPost attributes there is a respective attribute for the HTTP Delete Method - HttpDelete. You can add this attribute to get controller method so that it will respond to a DELETE Request:

[HttpDelete("/{id}")]
public async Task Delete([FromRoute] string id)
{
    await Task.CompletedTask;
}

In the above we make a simple Delete method and decorate it with the HttpDelete attribute. It takes an id as input in its query string, locally I can call the above using https://localhost/123 when running the above.

Having a body for the DELETE request

Alternatively to having the id as part of the route (aka path) you can have it as part of a body. Below is an example of this, Note: this might not always be the best practice:

[HttpDelete()]
public async Task Delete([FromBody] DeleteRequest requestBody)
{
    await Task.CompletedTask;
}

public class DeleteRequest
{
    public int Id { get; set; }
}

In the above we use the FromBody attribute instead of FromRoute and the input is now a class which will be a JSON body structure like the following:

{
  "id": 123
}

Making a DELETE request using the HttpClient in C#

I have made another post on how to call the above endpoints here. There is an example of calling the endpoint with and without a JSON body.

That is it

This was a short post on how to make an endpoint that can receive a HTTP Delete request in ASP.NET. Let me know in the comments what you think!