chr*_*dev 2 c# asp.net-web-api attributerouting asp.net-web-api-routing
我有一个Web API,看起来像以下......
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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用第一种方法时Get,这很好用.当我使用fiddler或Chrome REST Client调用第二种方法时GetMiniLeagueTable,我收到以下错误:
{消息:"请求无效." MessageDetail:"参数字典包含参数'type'的非可为空类型'CompetitionManager + MiniLeagueType'的空条目,方法'System.Collections.Generic.List`1 [竞争] Get(Int32,MiniLeagueType)''LeaguesController '.可选参数必须是引用类型,可以为空的类型,或者声明为可选参数." }
我AttributeRouting用来装饰方法,但这似乎不起作用.在我介绍之前它工作正常MiniLeagueType.
有人遇到过这个问题,还是你能指出我哪里出错了?
我认为原因是这个网址:api/Leagues/GetMiniLeagueTable/3.这个url匹配两个路由,因为它可以解释为: api/Leagues?type=GetMiniLeagueTable&id=3.但它无法转换GetMiniLeagueTable为CompetitionManager.MiniLeagueType值,因此会引发错误.
例如api/Leagues/GetCompetitions/{type}/{id},您应该创建更具体的路由,以防止URL匹配2个或更多不同的路由.
另一种可能性是颠倒你的动作指令,因为如果网址不匹配,它会在下一行之前检查第一个动作的路线.
| 归档时间: |
|
| 查看次数: |
2962 次 |
| 最近记录: |