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

前端 未结 4 1191
闹比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:22

    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 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

提交回复
热议问题