0
我想補充的OData查詢選項在現有的ASP.NET Web API服務2跟隨此link。我只希望查詢選項在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 $選擇=的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
}
嘗試改變[HTTPGET]至[EnableQuery]。您也可以嘗試自己應用這些選項,opts.SelectExpand.ApplyTo(order); – mumfy