簡體   English   中英

c# rest api 只獲取第一個值參數列表

[英]c# rest api only getting first value parameter list

我正在尋找一種方法來使我的 REST Api 在 C# 中能夠處理這樣的請求:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

現在我的 controller 中有這個:

[HttpGet]
[Microsoft.AspNetCore.Mvc.ProducesResponseType(typeof(Order), 200)]
[Route("api/Order/GetWithParam")]
public List<Order> GetDataWithParam(string orderIdString = null, DateTime? startDate = null, DateTime? endDate = null, string orderDescriptorsIdString = null)
    {
      ...

如果我的參數有一個或零個 arguments,這可以正常工作:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4

orderIdString 將等於“4”

但是,如果我為一個參數發送多個參數,則只會采用第一個參數:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

orderIdString 將等於“4”......但它應該等於“4,7”。

我錯過了什么?

- 編輯 -

我試圖改變:

string orderIdString = null

string[] orderIdString

但是現在 orderIdString 是 null 即使有參數傳遞

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

會給我 orderIdString == null

如果要綁定多個參數值,則應將類型設為數組( string[] ):

[HttpGet]
[Microsoft.AspNetCore.Mvc.ProducesResponseType(typeof(Order), 200)]
[Route("api/Order/GetWithParam")]
public List<Order> GetDataWithParam([FromQuery]string[] orderIdString = null, DateTime? startDate = null, DateTime? endDate = null, string orderDescriptorsIdString = null)
    {
      ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM