Question

I have some routes setup in my global.asax.cs file like this:

routes.MapHttpRoute(
            name: "ColorsRoute",
            routeTemplate: "api/{controller}/{action}/{styleId}",
            defaults: new
            {
                controller = "Cars",
                action = "Colors"
            }
        );

The controller:

    public Dictionary<string, string> Colors(string styleid)
    {
        //returns a dictionary of colors and ids
    }

The thing is when using a url like this:

localhost:58645/api/cars/colors/18752867

Doesnt pick up the styleId, but it works using it traditionally like this:

localhost:58645/api/cars/colors?styleid=18752867

Any ideas on why this is happening?

UPDATE:

These routes are working fine with calls like this "domain.com/api/cars/makes/new":

        routes.MapHttpRoute(
            name: "MakesRoute",
            routeTemplate: "api/{controller}/{action}/{state}",
            defaults: new
            {
                controller = "Cars",
                action = "Makes"
            }
        );

Controller:

    public Dictionary<string, string> Makes(string state)       
    {
        //return dictionary of makes and ids
    }

Here are the additional routes i have:

        routes.MapHttpRoute(
            name: "ModelsRoute",
            routeTemplate: "api/{controller}/{action}/{make}/{state}",
            defaults: new
            {
                controller = "Cars",
                action = "Models"
            }
        );

        routes.MapHttpRoute(
            name: "YearsRoute",
            routeTemplate: "api/{controller}/{action}/{modelId}/{state}",
            defaults: new {
                controller = "Cars",
                action = "Years"
            }
        );

        routes.MapHttpRoute(
            name: "StylesRoute",
            routeTemplate: "api/{controller}/{action}/{make}/{model}/{year}",
            defaults: new {
                controller = "Cars",
                action = "Styles"
            }
        );
Was it helpful?

Solution

Depending on the order that you have your routes defined, the matched route might not be the one you want your request to be sent to. Particularly with your YearsRoute and ModelsRoute. You have the same number of segments, but don't have any routing constraint's specified.

Try putting MapHttpRoute as your first registered route, and add a regular expression constraint

routes.MapHttpRoute(
        name: "ColorsRoute",
        routeTemplate: "api/{controller}/{action}/{styleId}",
        defaults: new
        {
            controller = "Cars",
            action = "Colors"
        },
      constraints: new { styleId = @"\d+" }
    );

See if it matches now.

UPDATE
Another option would be to define a route specifically for your URI that maps directly to your ApiController and Action method that you want to have used. That way requests always go to the action you want it to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top