[英]Use $select OData Query Option in ASP.NET Web API 2 service
我试图通过此链接在现有的ASP.NET Web API 2服务中添加OData查询选项。 我只希望查询选项在GET方法中可用。 尝试通过ID获取项目时,我无法使用$ select选项。 我该如何实现? 这是我到目前为止的内容:
public class OrdersController : ApiController
{
private readonly IOrderService orderService;
public OrdersController(IOrderService orderService)
{
this.orderService = orderService;
}
[HttpGet]
[Route("api/orders/{id}", Name = "GetOrderById")]
[ResponseType(typeof(Order))]
public IHttpActionResult GetOrder(ODataQueryOptions<Order> opts, [FromUri] int id)
{
Order order = orderService.GetOrderById(id);
if (order == null)
{
return NotFound();
}
if (opts.SelectExpand != null)
Request.ODataProperties().SelectExpandClause = opts.SelectExpand.SelectExpandClause;
return Ok(order);
}
}
当我测试它( http:// localhost:10240 / api / orders / 1 ?$ select = OrderId)时,我得到以下结果:
{
"OrderId": 1,
"ExternalId": "S001",
"TransactionType": "I",
"BusinessAssociateId": 1,
"DeliveryDate": "2017-06-30T21:08:50.427",
"Priority": 5,
"OrderType": "A",
"Status": "F",
"Information": "Incoming Material",
"Device": "1",
"BusinessAssociateName": null,
"BusinessAssociateStreet": null,
"BusinessAssociateCity": null,
"OrderDetails": null
}
正如@mumfy所建议的,我错过了应用选项的机会。 GetOrder方法的最后一行左:
返回Ok(opts.ApplyTo(Enumerable.Repeat(order,1).AsQueryable()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.