我看不出这里出了什么问题。我有更具体的路线,但它只返回错误404 - 未找到。
执行 / api / playernames / competitions 但 / api / playernames / teams / competitionid / 81bbd23d-54a2-4204-a771-85c48555a992 不起作用。我做错了什么?
routes.MapRoute("PlayerNamesDbTeams", "playernames/teams/competitionid/{competitionId}",
new { controller = "playernames", action = "Teams", competitionId = "" });
routes.MapRoute("default", "{controller}/{action}/{id}",
new { controller = "playernames", action = "Competitions" });
public class PlayerNamesController : ApiController
{
[HttpGet]
public List<Competition> Competitions()
{
using (var service = new AggregatorClient())
{
return service.GetCompetitions();
}
}
[HttpGet]
public List<Team> Teams(string competitionId)
{
using (var service = new AggregatorClient())
{
return service.GetTeams(competitionId);
}
}
}
提出 / api / playernames / teams?competitionId = xxxxxx 之类的请求确实有效。
答案 0 :(得分:1)
您似乎正在使用MVC路由;如果你正在使用ApiControllers,你应该使用WebApiConfig来定义你的路线。
打开WebApiConfig.cs
文件夹中的App_Start
并按如下方式定义路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "PlayerNamesDbTeams",
routeTemplate: "api/{controller}/{action}/competitionid/{competitionId}",
defaults: new { competitionId = "" }
);