C# 多重GET';Web API中的s调用错误操作
我有一个C# 多重GET';Web API中的s调用错误操作,c#,asp.net-web-api,asp.net-web-api-routing,attributerouting,C#,Asp.net Web Api,Asp.net Web Api Routing,Attributerouting,我有一个WebAPI,它看起来像以下内容 public class LeaguesController : ApiController { //api/Leagues/active/1 //api/Leagues/complete/1 //api/Leagues/both/1 [GET("api/Leagues/{type}/{id}")] public List<Competition> Get([FromUri]int id,
WebAPI
,它看起来像以下内容
public class LeaguesController : ApiController
{
//api/Leagues/active/1
//api/Leagues/complete/1
//api/Leagues/both/1
[GET("api/Leagues/{type}/{id}")]
public List<Competition> Get([FromUri]int id,
[FromUri]CompetitionManager.MiniLeagueType type)
{
return CompetitionManager.GetUsersMiniLeagues(id, true, type);
}
//api/Leagues/GetMiniLeagueTable/3
[GET("api/Leagues/GetMiniLeagueTable/{id}")]
public List<SportTableRow> GetMiniLeagueTable([FromUri]int id)
{
return SportManager.GetMiniLeagueTable("", id).TableRows;
}
}
公共类联盟控制器:ApiController
{
//api/Leagues/active/1
//api/Leagues/complete/1
//api/联盟/两者/1
[GET(“api/Leagues/{type}/{id}”)]
公共列表获取([FromUri]int-id,
[FromUri]CompetitionManager.MiniLeagueType)
{
返回CompetitionManager.GetUsersMiniLeagues(id,true,type);
}
//api/Leagues/GetMiniLeagueTable/3
[GET(“api/Leagues/GetMiniLeagueTable/{id}”)]
公共列表GetMiniLeagueTable([FromUri]int-id)
{
返回SportManager.GetMiniLeagueTable(“,id).TableRows;
}
}
当我调用第一个方法Get
时,它可以正常工作。
当我使用fiddler或Chrome REST Client调用第二个方法GetMiniLeagueTable
时,出现以下错误:
{Message:“请求无效。”MessageDetail:“参数无效。”
字典包含不可为null的参数“type”的null项
方法输入“CompetitionManager+小型联赛类型”
'System.Collections.Generic.List`1[Competition]Get(Int32,
“LeagueController”中的“MiniLeagueType”。必须输入可选参数
引用类型、可为null的类型或可声明为可选类型
参数“}
我正在使用装饰的方法,但这似乎不起作用。在我引入MiniLeagueType
之前,它运行良好
有人遇到过这个问题吗,或者你能告诉我哪里出了问题吗?我认为原因是这个url:
api/Leagues/GetMiniLeagueTable/3
。此url匹配两条路由,因为它可以解释为:api/Leagues?type=GetMiniLeagueTable&id=3
。但是它不能将GetMiniLeagueTable
转换为CompetitionManager.MiniLeagueType
值,因此会引发错误
您应该创建更具体的路由,例如api/Leagues/getcompetities/{type}/{id}
,以防止url匹配2个或更多不同的路由
另一种可能是颠倒你的行动顺序,因为如果url不匹配,它将在进行下一个行动之前检查第一个行动的路线。看起来url:/api/Leagues/GetMiniLeagueTable/3将匹配两个路线 假设它与第一个路由匹配,那么它将无法将GetMiniLeagueTable转换为CompetitionManager.MiniLeagueType,除非这是一个有效的枚举值 您可能需要首先测试第二个路由,并且只有当它与url不匹配时,才尝试第二个路由 我自己没有使用attributerouting(尽管我在最新的web api中使用了类似的属性路由),我想这会有所帮助 试一试 及
你怎么称呼它。有些东西不应该为null,因为null我不知道反转操作会做到这一点,但现在它是有意义的…这已经起作用了!
[GET("api/Leagues/{type}/{id}", ActionPrecedence = 2)]
[GET("api/Leagues/GetMiniLeagueTable/{id}", ActionPrecedence = 1)]