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

前端 未结 4 1190
闹比i
闹比i 2021-02-03 21:15

I have configured my WebApiConfig like this:

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: \"DefaultApi\"         


        
4条回答
  •  滥情空心
    2021-02-03 21:28

    Try changing your Controller method as

    public IEnumerable 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 loc = new List();
    
        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

提交回复
热议问题