。NET Core大张旗鼓的查询字符串参数

问题描述 投票:1回答:1

我有一个名为“ FootballControler”的控制器,具有以下端点:

  • 获取/ 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. 
}

请帮助我解决此问题。谢谢

c# asp.net-core .net-core swagger swashbuckle
1个回答
0
投票

使用一个具有多个参数的功能。对于没有值的参数,您的函数将收到null。

[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. 
}

© www.soinside.com 2019 - 2024. All rights reserved.