1

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; }
}

1 Answer 1

1

In your WebApiConfig class, before the "DefaultApi" route you can register your route as follows:

config.Routes.MapHttpRoute(
    name: "GetGamesRoute",
    routeTemplate: "api/games/{deviceId}",
    new { controller = "games", action = "get", deviceId = RouteParameter.Optional }
);

// This is the default route
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Also, make the deviceId parameter optional:

// GET api/Games/0xa16
public IEnumerable<Game> Get(string deviceId = null)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.