1

Using web-api i go to /api/cat/orders/

I specified the following routes and controller methods. I expected that the "ApiRouteWithCategoryAndExtId" route and the "GetMessageByCategoryExtId" method would be used since I made "extid" optional. But it is using the default Get method.

(/api/cat/orders/id18 uses GetMessageByCategoryExtId, but then extid is not optional)

What am I doing wrong?

Routes:

config.Routes.MapHttpRoute(
    name: "ApiRouteUniqueId",
    routeTemplate: "api/def/{id}",
    defaults: new { id = RouteParameter.Optional, controller = "Default" }
);

config.Routes.MapHttpRoute(
    name: "ApiRouteWithCategoryAndExtId",
    routeTemplate: "api/cat/{category}/{extid}",
    defaults: new { extid = RouteParameter.Optional, controller = "Default"}
);

Controller:

public string Get()

public HttpResponseMessage GetMessageById(int id)

public HttpResponseMessage GetMessageByCategoryExtId(
    string category, string extid)
2
  • 1
    What is the default route, and is it defined before or after these other routes? Commented Mar 8, 2013 at 3:58
  • These are the only two routes, so I suppose the first is the default? It might have something to do with te web-api conventions, or just the fact that it matches the first route based on pattern, not on actual content. But I don't know how I check what is happening here. Commented Mar 8, 2013 at 19:03

1 Answer 1

1

Here you would need to specify a default value for extid:

public HttpResponseMessage GetMessageByCategoryExtId(
    string category, string extid **= null**)

if the above default value is not specified, webapi tries to do a strict matching of an action's parameters with available values(via route parameters or query string values) and since here your url here /api/cat/orders/ does not have a value for 'extid', it isn't present in the route values and hence webapi cannot match to GetMessageByCategoryExtId.

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.