Web API route to action name

前端 未结 4 1657
刺人心
刺人心 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 09:44

    Adding additional information on above answer. Adding RoutePrefix would fix the issue sometimes. Hope it helps

    Controller

        [RoutePrefix("api/Services")]
        public class ServicesController : ApiController
        {
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            [System.Web.Http.HttpGet]
            [Route("MethodFruit")]
            public string[] MethodFruit()
            {
                return new string[] { "Apple", "Orange", "Banana" };
            }
        }
    

    In config

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

提交回复
热议问题