Can I have Multiple Get Methods in ASP.Net Web API controller

前端 未结 2 361
闹比i
闹比i 2021-01-13 05:35

I want to implement multiple Get Methods, for Ex:

Get(int id,User userObj) and Get(int storeId,User userObj)

Is it possibl

2条回答
  •  时光取名叫无心
    2021-01-13 06:17

    You need to add action name to the route template to implement multiple GET methods in ASP.Net Web API controller.

    WebApiConfig:

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

    Controller:

    public class TestController : ApiController
    {
         public DataSet GetStudentDetails(int iStudID)
         {
    
         }
    
         [HttpGet]
         public DataSet TeacherDetails(int iTeachID)
         {
    
         }
    }
    

    Note: The action/method name should startwith 'Get', orelse you need to specify [HttpGet] above the action/method

提交回复
热议问题