So this has really been causing me a headache.
Route:
config.routes.MapHttpRoute(
name: 'ControllerAction' ,
routeTemplate: "api/{controller}/{action}"
);
Controller:
public class LookupsController : ApiControllerBase
{
[ActionName("all")]
public Lookups GetLookups()
{
var lookups = new Lookups
{
Customers = GetCustomers().ToList(),
//add more
};
return lookups;
}
}
but whenver I try to hit the uri: /api/lookups/all I get a 404 error saying:
"No action was found on the controller 'Lookups' that matches the name 'all'."
Any help would be appreciated
EDIT: So I figured it out finally. it was because of the wrong depency. VS2012 autoresolved the action to system.web.mvc.actionnameattribute while what I needed was system.web.http.actionnameattribute.
weird problem, anyways, I hope this helps someone else.
Jakob Li,
EDIT:
You are renaming the controller but not specifying the method. WebApi resolves the method from the prefix GET, PUT, DELETE, POST. So the method attribute must be placed at the signature of the action:
Try this, works well for me:
public class LookupsController : ApiControllerBase
{
[HttpGet()]
[ActionName("All")]
public Lookups GetLookups()
{
var lookups = new Lookups
{
Customers = GetCustomers().ToList(),
//add more
};
return lookups;
}
}
routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Take a look here for more information about routing:
Hopes its help you!