简体   繁体   English

不同路由的同一控制器中的 AmbiguousActionException

[英]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.

相关问题 同一个控制器中的AmbiguousActionException - AmbiguousActionException in same controller Controller 找不到方法-AmbiguousActionException - Controller Method not found -AmbiguousActionException SetActualResponseType使用相同方法在同一控制器中使用不同的路由 - SetActualResponseType with same method in the same controller with different routes 将两条不同的路线映射到同一控制器动作 - Map two different routes to the same controller action 如何为具有不同参数的同一Controller方法设置不同的路由? - How to set different routes for the same Controller method with different parameters? 如何在同一控制器的不同路由中封装通用请求处理 - How to encapsulate common request handling in different routes in same controller 以与控制器相同的前缀开头的路由不起作用 - Routes that starts with the same prefix as controller not working 不同的RoutePrefix,相同的控制器名称 - Different RoutePrefix, same controller name 同一控制器的不同类型的身份验证 - Different type of authentication for the same controller 是否可以让多个MVC路由指向同一个控制器/视图? - Is it possible to have multiple MVC routes point to the same controller/view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM