Web API route to action name

前端 未结 4 1659
刺人心
刺人心 2020-12-14 09:11

I need a controller to return JSON to be consumed by JavaScript so I inherited from the ApiController class but it isn\'t behaving as I expected. The Apress bo

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 10:07

    If you want Web API to look for the action name when routing, change the WebApiConfig.cs class in the App_Start folder to this:

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

    Then you can just make a GET request to

    http://mysite/api/Services/MethodFruit
    

提交回复
热议问题