2017-08-21 6 views
0

このlinkに続いて、既存のASP.NET Web API 2サービスにODataクエリオプションを追加しようとしています。私はクエリオプションがGETメソッドで利用できるようにしたいだけです。 idで項目を取得しようとすると、$ selectオプションを使用することができませんでした。どうすればこれを達成できますか?私はそれをテストする場合(http://localhost:10240/api/orders/1 $ = OrderIdでの選択?)

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); 
    } 
} 

私は、次のような結果があります:これは私がこれまで持っているものである@mumfyが示唆したように、私は適用するために逃し、

{ 
    "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 
} 
+0

[HttpGet]を[EnableQuery]に変更してみてください。オプションを自分で試してみることもできます。opts.SelectExpand.ApplyTo(order); – mumfy

答えて

0

をオプション。 GetOrderメソッドの最後の行は、残っています。

戻り値Ok(opts.ApplyTo(Enumerable.Repeat(order、1).AsQueryable()));

関連する問題