why action parameter converts to query string in asp net core

Viewed 26

I have this method in ASP.NET Core:

   [HttpDelete("id:int")]
    public async Task<IActionResult> Delelte(int id)
    {
       // do sth
    }

Now when I call this api via swagger the generated url is :

https://localhost:44357/api/mycontroller/id:int?id=2

I expected to have a url without querystring like api/mycontroller/2

The problem is that I want to call this api from code and in code I call it like this api/mycontroller/2 but it return 404

2 Answers
[HttpDelete("id:int")]

Should be

[HttpDelete("{id:int}")]

The parameters are encapsulated in angle brackets {} with optional constraints like int. Everything outside of that is considered a static part of the route url.

See also Routing in ASP.NET Core

The issue seems to come from the way you have fromatted your HttpDelete attribute.

Try to format it this way instead :

[HttpDelete("{id}")]
Related