ASP.NET core HttpGet single Web API

烂漫一生 提交于 2019-12-02 04:55:43

If you want to map it to api/Controller/method/id you would need to use the code below because you want to map parameter order (no other identifier) to a specific parameter name in the action.

[HttpGet("GetCashMovement/{id}")]

Your current code should work with below since you are using named parameters and because the request can't be mapped to any other template.

/api/CashMovements/GetCashMovement?id=1

But that attribute syntax will also (possibly unintentionally) trigger:

/api/CashMovements/1

Since a sum of your defined template for that action is:

[Route("api/[controller]/{id}")]

Reason to why /api/ApiTest/GetCashMovement maps GetCashMovement.Get(int i) is because id is defined as optional in startup

routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/**{id?}**");

A question mark (?) after the route parameter name defines an optional parameter.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-3.0#create-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!