7
votes

New to .NET Core.

What am I doing wrong? Here is my controller

[Route("api/[controller]")]
public class customerController : Controller
{
    // GET: api/values
    //[HttpGet]
    //public IEnumerable<string> Get()
    //{
    //    return new string[] { "value1", "value2" };
    //}

    // GET api/values/5
    [HttpGet("{id}")]
    public customer Get(int id)
    {
        var repo = new customerRepository();
        return repo.GetCustomerOnPhone(id);
    }
}

I am able to call the API with this URL

http://localhost:51375/api/customer/8172858817

I hit the breakpoint in GET method, but Id is always zero. I cannot get the method to read the value which is being passed from URL.

Any suggestions?

1
Max int is 2147483647 Your value is too large. change to long - Nkosi
Or use Guid for Id's - Antoshjke

1 Answers

6
votes

int MaxValue = 2147483647

Your value 8172858817 is too large.

Either use a smaller value or change parameter to long

[HttpGet("{id:long}")]
public IActionResult Get(long id) {
    var repo = new customerRepository();
    customer model = repo.GetCustomerOnPhone(id);
    return Ok(model);
}

Reference Routing in ASP.NET Core : Route Constraint Reference