I have an existing WebAPI 2 project that has the current routing :
config.Routes.MapHttpRoute
(
name: "API",
routeTemplate: "api/{controller}/{id}/{function}",
defaults: new { id = RouteParameter.Optional, function = RouteParameter.Optional }
);
The controllers consist of a generic basecontroller and derived controllers per "entity type" that implement the routes :
[GET] api/{entity}/ <- returns an overview list of entities
[GET] api/{entity}/{id} <- returns the full entity + details
[POST] api/{entity}/{id} <- saves the entity
[DEL] api/{entity}/{id} <- deletes the entity
[POST] api/{entity}/ <- creates a new entity
[POST] api/{entity}/{id}/{function} <- performs a function on an entity (eg. recalculate, send orders,..)
Now i want to add a new method to my basecontroller to be able to get the "count" for an overviewlist. So basically
[GET] api/{entity}/count
I've added the following route to the webapi config :
config.Routes.MapHttpRoute
(
name: "count",
routeTemplate: "api/{controller}/", defaults: new { action = "count" }
);
and added a method to my controller:
[HttpGet]
public async Task<int> Count()
{
return 5;//just a fixed testvalue
}
If i now browse to /api/{entity}/count
, i get the value "5" returned.
But the problem is that the overviewlist /api/{entity}/
is no longer working. It says :
ExceptionMessage: "Multiple actions were found that match the request"
I've tried paying around with the "Route" attribute and and the order of the routes, but I cannot get it as I want (which is: everything working like before + the addition of the "count" in the API). I've also looked around on SO and found threads like How to add custom methods to ASP.NET WebAPI controller? but I still can't get working :(
Any idea ?
Thnx.