Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No HTTP resource was found that matches the request URI in Web API

I have configured my WebApiConfig like this:

public static void Register(HttpConfiguration config) {     config.Routes.MapHttpRoute(         name: "DefaultApi",         routeTemplate: "api/{controller}/{action}/{id}",         defaults: new { id = RouteParameter.Optional }     ); } 

I have one method which accepts one parameter. The accessing URI is http://localhost:8598/api/WebApi/GetLocationCategory/87.

This gives me an error: No HTTP resource was found that matches the request URI 'http://localhost:8598/api/WebApi/GetLocationCategory/87'

Controller:

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int CatID) {     var LocCats = (from lct in entities.tdp_LocationCategories join lc in entities.tdp_LocationMaster on lct.FK_LocationID equals lc.LocationID where lct.IsApproved == 0 && lct.FK_CategoryID == CatID select new { lc.LocationID, lc.LocationName }).ToList();     List<LocationCategory_CLS> loc = new List<LocationCategory_CLS>();      foreach (var element in LocCats)     {         loc.Add(new LocationCategory_CLS         {             LocationID = element.LocationID,             LocationName = element.LocationName         });     }     return loc; } 
like image 843
DharaPPatel Avatar asked Mar 30 '13 06:03

DharaPPatel


2 Answers

Try changing your Controller method as

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int id) <-- Change {     var LocCats = (from lct in entities.tdp_LocationCategories join lc in entities.tdp_LocationMaster on lct.FK_LocationID equals lc.LocationID where lct.IsApproved == 0 && lct.FK_CategoryID == id select new { lc.LocationID, lc.LocationName }).ToList();     List<LocationCategory_CLS> loc = new List<LocationCategory_CLS>();      foreach (var element in LocCats)     {         loc.Add(new LocationCategory_CLS         {             LocationID = element.LocationID,             LocationName = element.LocationName         });     }     return loc; } 

The change is only, changing input parameter from CatId to id.... It works for me many times..

Edit :


Its a long time when I look back I think I know the reason now. Words Like Jared is correct, it's all to do with Routing which we specify. If I have a route(default) as :

routes.MapRoute(         "Default",                                              // Route name         "{controller}/{action}/{id}",                           // URL with parameters         new { controller = "Home", action = "Index", id = "" }  // Parameter defaults     ); 

And my URL is /MyController/GetLocationCategory/123, it will be equivalent to /MyController/GetLocationCategory?id=123.

Similarly, if I want to change my parameter name for Id to say CatId, then I need to change the query string parameter(the way I am calling my Controller Action would change). Which would now be :

/MyController/GetLocationCategory?CatId=123

like image 111
Shubh Avatar answered Sep 18 '22 02:09

Shubh


config.Routes.MapHttpRoute(             name: "DefaultApi",             routeTemplate: "api/{controller}/{action}/{id}",             defaults: new { id = RouteParameter.Optional }         ); 

Maps to:

http://localhost:8598/api/Controller/Action/id

The WebApi portion of the url is redundant with api. Then modify the method parameter name to match the route:

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int id)

This is a good default as it matches the convention.

Alternatively, you can modify the route to use this unconventional parameter name instead:

config.Routes.MapHttpRoute(             name: "DefaultApi",             routeTemplate: "api/{controller}/{action}/{CatID}",             defaults: new { CatID = RouteParameter.Optional }         ); 

Finally, in either case make sure the controller name ends in Controller.

Good names: CustomController, CustomApiController
Bad names: Custom, CustomApi

like image 29
P.Brian.Mackey Avatar answered Sep 18 '22 02:09

P.Brian.Mackey