1

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"
            }
        );
1
  • you said you have some routes, but you only show one. Are there other routes in your Global.Asax Commented Apr 15, 2012 at 14:34

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

It turns out the MakesRoute was conflicting with it so i physically defined the controller and action which made it work. If you update your answer with something along those lines i will except it.

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.