[英]AmbiguousActionException in same controller for different routes
I have a controller getting AmbiguousActionException.我有一个控制器得到 AmbiguousActionException。 I have 3 methods I want to perform 1. Get a member based on their Id 2. Find all members with certain eye color 3. Get all members
我有 3 种方法要执行 1. 根据他们的 Id 获取成员 2. 找到具有特定眼睛颜色的所有成员 3. 获取所有成员
The first works fine but 2 and 3 get AmbiguousActionException I assume because they both only use HTTPGet and nothing else.第一个工作正常,但我假设 2 和 3 得到 AmbiguousActionException,因为它们都只使用 HTTPGet 而没有其他任何东西。 Is there a way I can differentiate between a default HTTPGet with a query parameter and HTTPGet.
有没有办法区分带有查询参数的默认 HTTPGet 和 HTTPGet。 I'm using a query parameter as the eyecolor filter criteria, not a key for the member.
我使用查询参数作为眼睛颜色过滤条件,而不是成员的键。
Here is my code:这是我的代码:
namespace MyController.Controllers
{
[Route("members")]
public class MemberController: Controller
{
[HttpGet("{memberId}")]
public IActionResult GetMemberFor(string memberId)
{
return Ok(DoSomeMethodWithId(memberId));
}
[HttpGet(Name = "FindMembersWith")]
public IActionResult FindMembersFor([FromQuery(Name = "eyeColor")] string eyeColor)
{
return Ok(DoSomeOtherMethodWithFilter(eyeColor);
}
[HttpGet(Name = "GetAllMembers")]
public IActionResult GetAllMembers()
{
return Ok(DoMethodToRetrieveAllMembers);
}
}
}
urls:
www.mysite.com/members/{id} -- get single member by id
www.mysite.com/members -- get all members
www.mysite.com/members?eyeColor=blue --get all members with blue eyes
Is there something I can add to make the 2nd and 3rd routes work?我可以添加一些东西来使第二条和第三条路线工作吗?
You can add custom routes:您可以添加自定义路由:
[Route("FindMembersWith")]
[HttpGet]
public IActionResult FindMembersFor([FromQuery(Name = "eyeColor")] string eyeColor)
{
return Ok(DoSomeOtherMethodWithFilter(eyeColor);
}
[Route("GetAllMembers")]
[HttpGet]
public IActionResult GetAllMembers()
{
return Ok(DoMethodToRetrieveAllMembers);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.