我可以在ASP.net core 3.0的[Route(“ x / {id}”))中使用变量吗?

时间:2019-11-20 11:18:10

标签: c# json asp.net-core-3.0

我正在制作“客户”仪表板。在此仪表板中,每个客户都有一个或多个联系人。这些联系人有自己的屏幕。为了获得进入该屏幕的权限,我想我将在其中实施一条带有所选客户ID的路​​线。现在,我必须手动插入Customerid。我希望这是自动的。我该怎么办?

我的代码:

客户

public CustomerController()
        {
            _customers = new List<Models.Customer>();
            _customers.Add(new Models.Customer { Id = 1, Email = "Test1@gmail.com", Name = "Test1", Password = "Test1"});
            _customers.Add(new Models.Customer { Id = 2, Email = "Test12@gmail.com", Name = "Test12", Password = "Test12" });
            _customers.Add(new Models.Customer { Id = 3, Email = "Test123@gmail.com", Name = "Test123", Password = "Test123" });
            _customers.Add(new Models.Customer { Id = 4, Email = "Test1234@gmail.com", Name = "Test1234", Password = "Test1234" });
            _customers.Add(new Models.Customer { Id = 5, Email = "Test12345@gmail.com", Name = "Test12345", Password = "Test12345" });
        }

        [HttpGet]
        public IActionResult GetCustomers()
        {
            return Ok(_customers);
        }

        [HttpGet("{id}")]
        public IActionResult GetCustomer(int id)
        {
            foreach(var item in _customers)
            {
                if(id == item.Id)
                {
                    return Ok(item);
                }
            }
            return NoContent();
        }

联系人:

 [Authorize]
    [Route("customer/{customerid}/contact")]
    [ApiController]
    public class ContactController : ControllerBase
    {
        private List<Models.Contact> _contacts;

        public ContactController()
        {
            _contacts = new List<Models.Contact>();
            _contacts.Add(new Models.Contact { Id = 1, Email = "Test1@gmail.com", Name = "Test1", Password = "Test1", Phone = 11111 });
            _contacts.Add(new Models.Contact { Id = 2, Email = "Test12@gmail.com", Name = "Test12", Password = "Test12", Phone = 1111 });
            _contacts.Add(new Models.Contact { Id = 3, Email = "Test123@gmail.com", Name = "Test123", Password = "Test123", Phone = 111 });
            _contacts.Add(new Models.Contact { Id = 4, Email = "Test1234@gmail.com", Name = "Test1234", Password = "Test1234", Phone = 11 });
            _contacts.Add(new Models.Contact { Id = 5, Email = "Test12345@gmail.com", Name = "Test12345", Password = "Test12345", Phone = 1 });

        }
        [HttpGet]
        public IActionResult GetCustomers()
        {
            return Ok(_contacts);
        }

        [HttpGet("{id}")]
        public IActionResult GetContact(int id)
        {
            foreach (var item in _contacts)
            {
                if (id == item.Id)
                {
                    return Ok(item);
                }
            }
            return NoContent();
        }

1 个答案:

答案 0 :(得分:3)

您可以在ContactController上将路线数据移至操作中,并保持路线简单。

例如:

[Route("contact")]
[ApiController]
public class ContactController : ControllerBase

    [HttpGet("{customerId}/{contractId}")]
    public IActionResult GetContact(int customerId, int contactId)
    {

    }

然后您可以通过contact/1/1进行访问,这将为您提供客户ID == 1和联系人ID == 1