This question is pretty close to this one, with a minor twist.
I would like to set up a Web API routing table so I can make a request like api/items/5
as well as api/items/newest
so the client can get the newest item.
I've been paging through some articles but haven't had much luck in determining whether I'm going about it the right way.
My first question is, what's the "right" or "clean" way to allow a caller to find the newest item
? Is it OK to do something like api/items/newest
or is that frowned upon?
Currently, this seems to work in my WebApiConfig.cs file:
config.Routes.MapHttpRoute(
name: "NewestItems",
routeTemplate: "api/items/newest",
defaults: new { controller = "Items" }
);
config.Routes.MapHttpRoute(
name: "ItemsById",
routeTemplate: "api/items/{id}",
defaults: new { controller = "Items" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
This approach works, but I'm curious if there's a more generic way to do this without making a controller-specific routing rule. I had tried the following approach, but it didn't work since the rules were ambiguous:
config.Routes.MapHttpRoute(
name: "ActionWithNoArgument",
routeTemplate: "api/{controller}/{action}",
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Is there a more effective way to do this without resorting to controller-specific routing rules?
Thanks guys!