I expect following behaviour: when calling http://localhost:59060/api/games/ - it returns all games, when calling http://localhost:59060/api/games/aaa - it returns all games where DeviceId = aaa.
Currently when I call http://localhost:59060/api/games/aaa or http://localhost:59060/api/games/, error is:
No HTTP resource was found that matches the request URI
http://localhost:59060/api/games/No action was found on the controller 'Games' that matches the request.
Controller
public class GamesController : ApiController
{
private List<Game> _GamesRepository;
public GamesController()
{
_GamesRepository = CreateGamesRepository();
}
// GET api/Games/0xa16
public IEnumerable<Game> Get(string deviceId)
{
if (String.IsNullOrEmpty(deviceId))
{
return _GamesRepository;
}
else
{
return _GamesRepository.Where(x => x.DeviceId == deviceId);
}
}
}
Configuration
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ControllerDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ApiDefault2",
url: "api/{controller}/{action}/{deviceId}",
defaults: new { action = "get", deviceId = UrlParameter.Optional }
);
Model
public class Game
{
public int TaskId { get; set; }
public string SalesForceId { get; set; }
public string Name { get; set; }
public string Thumbnail { get; set; }
public string DeviceId { get; set; }
}