【问题标题】:GET action with multiple ids inputted .NET输入多个 id 的 GET 操作 .NET
【发布时间】:2018-06-15 10:40:26
【问题描述】:

是否可以根据输入的多个 id 创建一个 GET 操作?

例如,我怎样才能将此方法更改为 GetCustomer([FromRoute] int id, int code_id)?

// GET: api/Customer/5
[HttpGet("{id}")]
public async Task<IActionResult> GetCustomer([FromRoute] int id)
    {
      if (!ModelState.IsValid)
      {
         return BadRequest(ModelState);
      }

      var customerMaster = await _context.CustomerMaster.SingleOrDefaultAsync(m => m.Code == id);

      if (customerMaster == null)
      {
         return NotFound();
      }

      return Ok(customerMaster);
 }

【问题讨论】:

  • 是的,一个方法可以有多个参数。额外的 GET 参数要么在路由中,要么在查询字符串中。
  • 我只是想知道我对 -1 的问题有什么问题?

标签: c# .net get


【解决方案1】:

使用属性路由:

// GET: api/Customer/5/3

[Route("api/Customer/{id}/{code_id}")]
public async Task<IActionResult> GetCustomer(int id, int code_id)
{

        ...
   return Ok(customer);
}

【讨论】:

  • 我怎样才能在这里连接它们:(m => m.Code == id)?用一个简单的 &&?
  • 可以,可以使用&&,即(m => m.Code == code_id && m.Id == id)
  • 如果我不想返回我只是他第一个是/1/2,我应该怎么做?跨度>
  • 使用.ToList() 并返回列表,而不是单个对象,即List&lt;Customer&gt; customers = context.Customers.Where(m =&gt; m.Code == code_id &amp;&amp; m.Id == id).toList();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多