我该如何设置多个操作方法来处理仅通过传递参数才不同的获取请求? [英] how can I setup multiple action methods to handle get request that is only differential by the passing parameters?
本文介绍了我该如何设置多个操作方法来处理仅通过传递参数才不同的获取请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是ASP.NET MVC的新手,下面是我的api控制器:
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)
{
....
}
然后,当我开始应用并路由到localhost/api/student
时,它抛出了一个异常,即发现有多个与请求匹配的动作
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?
推荐答案
尝试这样可以正常工作
[HttpGet("Get")]
public JsonResult Get()
{
....
}
[HttpGet("Get/{id}")]
public JsonResult Get(int id)
{
//....
}
[HttpGet("GetFromQuery")]
public JsonResult Get([FromQuery]string sortby)
{
....
}
这篇关于我该如何设置多个操作方法来处理仅通过传递参数才不同的获取请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文