I have 3 WEB API and 2 of them are working. The third one is not working although it is similar the other ones.
My Controller:
public class LNAXController : ApiController
{
[HttpGet]
public IEnumerable<State> States()
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
return db.States.Where(s => s.Country.Code == "USA").OrderBy(o => o.Name).ToList();
}
}
[HttpGet]
public IEnumerable<State> States(string countryCode)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
return db.States.Where(s => s.Country.Code == countryCode).OrderBy(o => o.Name).ToList();
}
}
[HttpGet]
public IEnumerable<City> Cities(int stateId)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
return db.Cities.Where(c => c.State.Id == stateId).OrderBy(o => o.Name).ToList();
}
}
}
The first and second API, return result using following urls I type in browser:
http://localhost:58211/api/lnax/states
http://localhost:58211/api/lnax/states/USA
But the 3rd one returns error when I use this url: http://localhost:58211/api/lnax/cities/5
No HTTP resource was found that matches the request URI 'http://localhost:58211/api/lnax/cities/5'.
No action was found on the controller 'LNAX' that matches the request.
Edit:
This is my configuration code:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "Get", id = RouteParameter.Optional }
);
}
}