簡體   English   中英

多個可選查詢字符串參數REST API GET

[英]Multiple optional query string parameters REST API GET

我正在使用web api 2來實現一個寧靜的服務。 在對最佳實踐進行一些研究之后,每個人似乎都對如何做以下事情有不同的看法。 我有一個GET

public HttpResponseMessage Get(string crewId, string shiftDate, int offset = 1, int limit = 10)

此GET方法返回一個列表。 有多種方法可以從此方法獲取數據。

  • 僅限crewId
  • 僅限shiftDate
  • 要么
  • 通過crewId和shiftDate獲取
  • 你(1)將crewId和shiftDate標記為可選嗎?

    if(crewId != null && shiftDate == null){
      // Get by crewId
    }else if(crewId == null && shiftDate != null){
      // Get By shiftDate
    }else if(crewId != null && shiftDate != null){
      // Get By crewId and shiftDate
    }
    

    然后有一堆if語句來檢查填充的內容和未填充的內容以便能夠執行操作

     if(crewId != null && shiftDate == null){ // Get by crewId }else if(crewId == null && shiftDate != null){ // Get By shiftDate }else if(crewId != null && shiftDate != null){ // Get By crewId and shiftDate } 

    對我來說這看起來很瘋狂,特別是如果你有很多參數,你的代碼中會有太多“if”語句。

    你(2)有不同的獲取?

     public HttpResponseMessage GetByCrewId(string crewId, int offset = 1, int limit = 10) public HttpResponseMessage GetByShiftDate(string shiftDate, int offset = 1, int limit = 10) public HttpResponseMessage GetByCrewIdShiftDate(string crewId, string shiftDate, int offset = 1, int limit = 10) 

    然后你將URI路由映射到方法

  • ... / API / GetByCrewId?crewId = 1234
  • ... / API / GetByShiftDate?shiftDate = 1111年11月11日
  • ... / API / GetByCrewIdShiftDate?crewId = 1234&shiftDate = 1111年11月11日
  • 選項2是否寧靜?

    或者有更好的選擇(3)。

    上述兩個選項都可以確保我使用最佳實踐並遵循REST標准。 看起來我錯過了什么,希望你能把我放在正確的方向。

    您不希望選項(2) - 在您的URL中想要名詞是RESTful。

    所以你希望你的網址看起來像:

    /api/items/?crewId=1234&shiftDate=1111-11-11
    

    (根據'Crew'和'Shift'參數名稱,我無法弄清楚你的物品是什么。什么有船員和班次日期?釣魚之旅?如果是這樣,網址會更好/ / api /釣魚人次/?crewId = ....&shiftDate = ...

    至於控制器,我會去做類似的事情:

    public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10) {
    
        return dataSource.Where(x=> (x.CrewId == crewId || crewId == null)
                && (x.ShiftDate == shiftDate || shiftDate == null));
    }
    

    查看最佳實踐:了解REST標頭和參數它指出使用查詢參數將指示它是可選的。 如果您可以創建單個值而其他值可選,則可能有助於闡明URI。

    /api/fishing-trips/crew/{crewid}?shiftdate=1111-11-11
    

    最后如果您的商品都是可選的,那么使用“?” 可能是最好的路線。 有關參數類型的更多信息,請參見RFC 6570

    請注意,您的選擇可能會影響您選擇使用的任何排隊,並且路徑樣式參數擴展可能最有意義。 有關REST參數的更多信息。

    最后,您可能希望將它們創建為搜索參數,如果您發現用戶經常請求相同的搜索,則可以將其打包到單個REST路徑中。

    例如,

    /api/fishing-trips/search?crew=1234
    /api/fishing-trips/search?shiftDate=1111-11-11
    /api/fishing-trips/search?crew=1234&shiftDate=1111-11-11
    

    您還可以提供簡化以及可選參數,例如,

    /api/fishing-trips/today
    /api/fishing-trips/today?crew=1234
    /api/fishing-trips/crew/1234/today
    

    這些最后的例子在我的研究中是主觀的,但更多信息可以在最佳實踐中用於Pragmatic Rest APIRESTful URL設計用於搜索

    我之前做過類似的事。 既然你可以使用其中一個或兩個,我會使用可選參數:

    public HttpResponseMessage Get(string crewId = null, string shiftDate = null, int offset = 1, int limit = 10)
    

    然后構建您的查詢。 例如,像這樣:

    var query = "";
    if (!String.IsNullOrEmpty(crewId)) {
      query += $"crewId='{crewId}'";
    }
    if (!String.IsNullOrEmpty(shiftDate)) {
      if (query.Length > 0) query += " AND ";
      query += $"shiftDate='{shiftDate}'";
    }
    if (query.Length == 0) {
      //neither crewId or shiftDate were given
      //return some kind of error
    }
    

    考慮到我自己即將實現這個,我會說它應該是一個帶有多個可選參數的單個GET方法動作。

    為什么? 因為您不必擔心REST API層中的此查詢邏輯。 畢竟,您正在有效地創建具有多個參數的AND辨別子句(即CrewId = 1 AND ShiftDate = 2016-01-01 )。 如果您不提供參數,請返回所有項目。

    我將把我的參數一直傳遞給SQL存儲過程,該過程具有指定的默認值,並將根據傳遞的參數返回結果。

    請記住,在許多方面,REST方法直接映射到CRUD,因此請將API視為: REST API教程

    暫無
    暫無

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

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