我无法让第二个控制器运行。我定义了第一个Controller并测试了它的所有方法没有问题。当我尝试在第二个控制器上调用方法时,我收到以下错误消息:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:9801/api/defense/getgameopponents/seattle seahawks'.
</Message>
<MessageDetail>
No action was found on the controller 'Defense' that matches the name 'get'.
</MessageDetail>
</Error>
我的第一个控制器 - PlayerController.cs
namespace Test.Controllers
{
public class PlayerController : ApiController
{
static readonly IPlayerRepository repository = new PlayerRepository();
public List<Player> GetPlayers(string position, string teamName)
{
return PlayerController.repository.GetPlayers(position, teamName);
}
public List<Defense> GetPlayersPastOpponents(string position, string teamName, string playerName)
{
return PlayerController.repository.GetPlayerPastOpponents(new Player(playerName, teamName, position));
}
public List<Player> GetPlayerStats(string position, string teamName, string playerName)
{
return new List<Player>(){ PlayerController.repository.GetPlayerStats(new Player(playerName, teamName, position)) };
}
}
}
功能失调的控制器
namespace Test.Controllers
{
public class DefenseController : ApiController
{
static readonly IDefenseRepository repository = new DefenseRepository();
string[] GetGameOpponents(string team)
{
return DefenseController.repository.GetGameOpponents(team);
}
Defense GetOpponentsStats(string teamName)
{
return DefenseController.repository.GetOpponentStats(new Defense(teamName));
}
}
}
最后,我的路由表。我有一种强烈的感觉,这是问题所在,但我不能指责它。这是我第一次创建这样的项目,所以任何帮助都会非常感激。
namespace Test
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "GetPlayerPastOpponentsApi",
routeTemplate: "api/{controller}/{action}/{position}/{teamname}/{playername}"
);
config.Routes.MapHttpRoute(
name: "GetPlayersApi",
routeTemplate: "api/{controller}/{action}/{position}/{teamname}"
);
config.Routes.MapHttpRoute(
name: "GetPlayerStatsApi",
routeTemplate: "api/{controller}/{action}/{position}/{teamname}/{playername}"
);
config.Routes.MapHttpRoute(
name: "GetGameOpponentsApi",
routeTemplate: "api/{controller}/{action}/{team}"
);
config.Routes.MapHttpRoute(
name: "GetOpponentsStatsApi",
routeTemplate: "api/{controller}/{action}/{opponentname}"
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableSystemDiagnosticsTracing();
}
}
}
答案 0 :(得分:1)
看起来您错过了操作方法的public
名称。
public /* <---THIS */ Defense GetOpponentsStats(string teamName)
{
return DefenseController.repository.GetOpponentStats(new Defense(teamName));
}