.Net 核心网站 API

.Net Core Web API

.NET Core 的新手。

我做错了什么?这是我的控制器

[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);
    }
}

我可以用这个 URL

调用 API

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

我在 GET 方法中打了断点,但 Id 始终为零。我无法获得读取从 URL.

传递的值的方法

有什么建议吗?

int MaxValue = 2147483647

您的值 8172858817 过大。

使用较小的值或将参数更改为 long

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

引用Routing in ASP.NET Core : Route Constraint Reference