If you use Web API 2, then you can use Attribute Routing to route requests like http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42
public class ItemsController : ApiController
{
[Route("api/{controller}/{id}")]
public string GetItemById(int id)
{
// Find item here ...
return item.ToString();
}
[Route("api/{controller}/{name}/{id}")]
public string GetItemByNameAndId(string name, int id)
{
// Find item here ...
return item.ToString();
}
}
http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42
will be mapped to GetItemByNameAndId
while http://192.112.183.42:80/api/platypusItems/42
will be mapped to GetItemById
.
Note, that you need to enable attribute routing in configuration like this:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
But generally you should pass arguments as additional parameters. It is especially easy with GET requests. This will work in Web API 1&2:
public class ItemsController : ApiController
{
public string GetItemById(int id)
{
// Find item here ...
return item.ToString();
}
public string GetItemByNameAndId(string name, int id)
{
// Find item here ...
return item.ToString();
}
}
Assuming that you have default mapping configuration, http://192.112.183.42:80/api/platypusItems/42
will be mapped to GetItemById
while http://192.112.183.42:80/api/platypusItems/42?name=DuckbilledPlatypisAreGuysToo
will be mapped to GetItemByNameAndId
because Web API can map 2 parameters instead of 1 for GetItemById
.
More information can be found in Mike Wasson articles on Attribute Routing, Routing and Action Selection and Routing in Web API.