Web API: Same Method with different HTTP Verbs

后端 未结 4 661
鱼传尺愫
鱼传尺愫 2021-01-06 12:48

In a WEB API controller, can we have the same method name with different HTTP Verbs?

  [HttpGet]
        public string Test()
        {
            return \"         


        
4条回答
  •  不要未来只要你来
    2021-01-06 13:47

    In config file don't break your default route.

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

    And I solve that problem with that way. Just use that lines before your methods start.

    [Route("api/Test")]
    [Route("api/Test/{id}")]
    

提交回复
热议问题