簡體   English   中英

.NET 核心 swagger 查詢字符串參數

[英].NET Core swagger query string params

我有一個名為“FootballControler”的 controller,具有以下端點:

  • 獲取 /api/FeedMatch
  • 獲取 /api/FeedMatch/ExternalMatchIdAndFeedLeagueId?externalMatchId=0&feedLeagueId=0
  • 獲取 /api/FeedMatch/ExternalMatchIdAndFeedTypeId?externalMatchId=0&feedTypeId=0
  • 獲取 /api/FeedMatch/feedMatchId=2
  • 獲取 /api/FeedMatch/MatchId=3
  • 獲取 /api/FeedMatch/dateMatch?dateMatch="2012/1/1"

如您所見,所有端點都試圖獲取 FeedMatch 列表。 我想要做的是讓它們像這樣:

  • 獲取 /api/FeedMatch
  • 獲取 /api/FeedMatch?externalMatchId=0&feedLeagueId=0
  • 獲取 /api/FeedMatch?externalMatchId=0&feedTypeId=0
  • 獲取 /api/FeedMatch?feedMatchId=2
  • 獲取 /api/FeedMatch?MatchId=3
  • 獲取 /api/FeedMatch?dateMatch="2012/1/1"

我的解決方案:

[HttpGet]
public List<Match> GetAll()
{
  return a list of matches. 
}

[HttpGet]
public List<Match> GetByMatchIdAndFeedLeagueId(string externalMatchId, int feedLeagueId)
{
  return a list of matches. 
}

[HttpGet]
public List<Match> GetByExternalMatchIdAndFeedTypeId(string externalMatchId, int feedTypeId)
{
  return a list of matches. 
}

[HttpGet]
public List<Match> GetByFeedMatchId(int feedMatchId)
{
  return a list of matches. 
}

[HttpGet]
public List<Match> GetByMatchId(int feedMatchId)
{
  return a list of matches. 
}

但是 Swashbuckle 在 asp.net 內核中失敗,出現 NotSupportedException 異常。

我不希望有一種帶有可選參數的方法,如下所示:

[HttpGet]
public List<Match> Get(int? feedMatchId = null, int? feedMatchId = null,string externalMatchId = null ..... )
{
  return a list of matches. 
}

請幫我解決這個問題。 謝謝

使用具有多個參數的 function。 您的 function 將收到無值參數的空值。

[HttpGet]
public List<Match> GetSomething(
[FromQuery] int? externalMatchId,
[FromQuery] int? feedLeagueId,
[FromQuery] int? feedTypeId)
{
  if (externalMatchId.HasValue)
  {
      select by match id 
  }
  ...
  return a list of matches. 
}

暫無
暫無

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

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