ASP.NET core HttpGet single Web API

后端 未结 1 1262
滥情空心
滥情空心 2021-01-25 19:03

Good Morning,

I’m having difficulty setting up my HTTPGETs and then testing the solution in Postman.

I’m trying to return a single result on both occasions howe

相关标签:
1条回答
  • 2021-01-25 19:45

    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

    0 讨论(0)
提交回复
热议问题