问题
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