Why having multiple GET methods with identical signatures and different action names doesn’t work

I've created two different GET endpoints with identical signatures, but with different action names as follows:

[HttpGet, ActionName("carinfo")]
public async Task<JsonResult> GetCarInfo(){...}

[HttpGet, ActionName("petinfo")]
public async Task<JsonResult> GetPetInfo(){...}

However, when I try and call them:

GET /api/v1/carinfo
GET /api/v1/petinfo

The calls being made are as expected (I'm able to see calls being made to /api/v1/carinfo & /api/v1/petinfo). However, after looking into what methods are actually being hit, I never actually hit GetPetInfo(), instead I hit GetCarInfo() twice for both calls. It's as if I had done two calls to /api/v1/carinfo instead of one and one.

I understand that one of the potential ways to solve this is would be to use Attribute routing to customize the endpoint URIs so that they would look as follows:

[Route("carinfo"), HttpGet]
public async Task<JsonResult> GetCarInfo(){...}

[Route("petinfo"), HttpGet]
public async Task<JsonResult> GetPetInfo(){...}

However, I wanted to know why having different action names isn't a valid solution and if there's another way to fix this without using attribute routing (ideally sticking within the System.Web Namespace).

Read more here: https://stackoverflow.com/questions/66265444/why-having-multiple-get-methods-with-identical-signatures-and-different-action-n

Content Attribution

This content was originally published by so_as at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: