0

I have 2 actions with the same name, but one is an overload of the first that takes an int parameter that the first overload does not have:

[HttpGet("Read")]
[Produces(typeof(IEnumerable<Client>))]
public async Task<IActionResult> Read()
...
[HttpGet("Read/{id}")]
[Produces(typeof(Client))]
public async Task<IActionResult> Read(int id)

I have tried every means I know to correctly define the route attribute template, and to form the correct URL to invoke the second action , with the parameter. Every try, using my code and PostMan, to invoke this action results in a 404.

The whole controller looks like this:

[Route("api/[controller]")]
[Produces("application/json")]
public class ClientsController : Controller
{
    private readonly IDataService _clients;

    public ClientsController(IDataService dataService)
    {
        _clients = dataService;
    }

    [HttpPost("Create")]
    [Produces(typeof(int))]
    public int Post([Bind("GivenName,FamilyName,GenderId,DateOfBirth,Id")] Client client)
    {
        var ret = _clients.Create(client);
        return ret;
    }

    [HttpGet("Read")]
    [Produces(typeof(IEnumerable<Client>))]
    public async Task<IActionResult> Read()
    {
        var clients = await _clients.ReadAsync();
        return Ok(clients);
    }

    [HttpGet("Read/{id}")]
    [Produces(typeof(Client))]
    public async Task<IActionResult> Read(int id)
    {
        var client = await _clients.ReadAsync(id);
        if (client == null)
        {
            return NotFound();
        }

        return Ok(client);
    }

    [HttpPut("Update")]
    public void Put([Bind("GivenName,FamilyName,GenderId,DateOfBirth,Id")] string json)
    {
        // NB Edit isn't saving.
        var client = JsonConvert.DeserializeObject<Client>(json);
        _clients.UpdateAsync(client);
    }

    [HttpDelete("Delete/{id:int}")]
    public void Delete(int id)
    {
    }
}
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • What is the url you are trying ? – Shyju Oct 29 '17 at 20:09
  • @Shyju I have tried two urls with no difference: `http://localhost:64232/api/clients/read/58`, and `http://localhost:64232/api/clients/58`. I know the API is working because when I call the one with no parameter, it works fine. Understand I tried nearly every combination of action url and route template. – ProfK Oct 29 '17 at 20:14
  • Have u tried to define the type in the route [HttpGet("Read/{id:int}")] ? – Marcus Höglund Oct 29 '17 at 20:17
  • @MarcusHöglund Yes, I have tried that with no difference. – ProfK Oct 29 '17 at 20:19
  • Did you enable Attribute Routing `routes.MapMvcAttributeRoutes();`? I've edited the link to point to exact answer. – Dmitry Oct 29 '17 at 20:23
  • @ProfK according to the logic in the action if the id does not exist you will also get the not found response. Have you tried in debug mode putting a break point to make sure that the id being called exists. The template in the question is correct. – Nkosi Oct 29 '17 at 20:24
  • 1
    @Dmitry I believe the OP is for asp.net-core while that last suggestion is for a previous version. – Nkosi Oct 29 '17 at 20:27
  • @Nkosi It is present, e.g. 58, 59 etc. – ProfK Oct 29 '17 at 20:33
  • That should work with the default route definition. – Shyju Oct 29 '17 at 20:47

0 Answers0