how can I setup multiple action methods to handle get request that is only differential by the passing parameters? [duplicate]

空扰寡人 提交于 2020-01-16 19:35:15

问题


I'm new to ASP.NET MVC, below is my api controller:

// API controller named Student

//match localhost/api/student
[HttpGet]
public JsonResult Get()
{
    ....       
}

//match localhost/api/student/123
[HttpGet("{id:int}")]
public JsonResult Get(int id) {
    //....         
}

//match localhost/api/student?sortby=grade
[HttpGet]
public JsonResult Get([FromQuery]string sortby)
{
  ....         
}

//match localhost/api/student?name=somename&gender=male
[HttpGet]
public JsonResult Get([FromQuery]string name, [FromQuery]string gender)
{
  ....         
}

then when I start the appication and routed to localhost/api/student, it threw an exception which is Multiple actions were found that match the request

so how can I setup mutiple get request that is differential by the passing parameters?


回答1:


try like this it will work

[HttpGet("Get")]
public JsonResult Get()
{
  ....       
}


[HttpGet("Get/{id}")]
public JsonResult Get(int id) 
{
   //....         
}


 [HttpGet("GetFromQuery")]
public JsonResult Get([FromQuery]string sortby)
{
   ....         
}


来源:https://stackoverflow.com/questions/59665394/how-can-i-setup-multiple-action-methods-to-handle-get-request-that-is-only-diffe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!