简体   繁体   English

.Net Core Web API

[英].Net Core Web API

New to .NET Core. .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 我可以使用此URL调用API

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

I hit the breakpoint in GET method, but Id is always zero. 我在GET方法中遇到断点,但Id始终为零。 I cannot get the method to read the value which is being passed from URL. 我无法获取方法来读取从URL传递的值。

Any suggestions? 有什么建议?

int MaxValue = 2147483647

Your value 8172858817 is too large. 您的值8172858817太大了。

Either use a smaller value or change parameter to long 要么使用较小的值,要么将参数更改为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 ASP.NET核心中的参考路由:路由约束参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM