I have configured my WebApiConfig like this:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: \"DefaultApi\"
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
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
what is the name of your controller and action ?
The URL should be
http://localhost:8598/api/Controller/Action
It does not map to the Route Configuration you have specified, hence the pipeline is unable to locate the correct Controller. The /id should not be in the path it must be in the body or the query parameters ( I got stumped in haste !!)
Example : -
public class FooController : ApiController
{
public int GetIndex(int id)
{
return id;
}
}
localhost:58432/api/foo/GetIndex?Id=1
O/P :- 1
Note:- If your action name is GetIndex
then the URL must be GetIndex
not just Index. Optionally you can specify the [HttpGet]
attribute for the action.
If you have the following as well in your WebApi.config
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Then http://localhost:58432/api/foo?Id=1
would also be a legitimate route. Not sure if you would want that.
Also, With WebAPI as far as possible stick to non action based routing , MVC is meant for that. It is not the recommended way.
Your request URL is http://localhost:8598/api/WebApi/GetLocationCategory/87
Your route is configured to accept: 'api/{controller}/{action}/{id}'
So you need to ensure that the name of your controller is 'WebApiController'.
Also, as stated alredy by @SDG you need to make sure that the name of the parameter to your action method matches what you have in your Route template i.e change 'CatID' to 'id'