C# 我如何设置多个操作方法来处理仅由传递的参数区分的get请求?
我是ASP.NET MVC新手,下面是我的api控制器:C# 我如何设置多个操作方法来处理仅由传递的参数区分的get请求?,c#,.net,asp.net-mvc,asp.net-core-mvc,C#,.net,Asp.net Mvc,Asp.net Core Mvc,我是ASP.NET MVC新手,下面是我的api控制器: // 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) { //....
// 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)
{
....
}
然后,当我启动应用程序并路由到localhost/api/student
时,它抛出了一个异常,即发现了多个与请求匹配的操作
那么,如何设置通过传递的参数进行区分的多个get请求呢
[HttpGet("Get")]
public JsonResult Get()
{
....
}
[HttpGet("Get/{id}")]
public JsonResult Get(int id)
{
//....
}
[HttpGet("GetFromQuery")]
public JsonResult Get([FromQuery]string sortby)
{
....
}
你能提供你的控制器代码吗