Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Web API

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?

like image 764
Coder77 Avatar asked Nov 24 '17 23:11

Coder77


1 Answers

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

like image 66
Nkosi Avatar answered Sep 20 '22 13:09

Nkosi