首页 > 解决方案 > Asp.net 核心路由 ambiguousActionException

问题描述

我有 2 controllers,我收到错误:

> AmbiguousActionException: Multiple actions matched. The following
> actions matched route data and had all constraints satisfied:
> WebApi.Controllers.BlacklistController.GetBlacklists (WebApi)
> WebApi.Controllers.WhitelistController.GetWhitelists (WebApi)

这是我的控制器

    [Produces("application/json")]
    [Route("api/infrastructure/blacklist")]
    public sealed class BlacklistController : Controller
    {
         private readonly IInfrastructure _infrastructure;
         public BlacklistController(IInfrastructure infrastucture) => _infrastructure = infrastucture;

         [HttpGet]
         [Route("/")]
         public async Task<IEnumerable<BlacklistedAgent>> GetBlacklists() => await _infrastructure.GetBlacklistedAgents().ConfigureAwait(false);
    }


WhitelistController.cs:

    [Produces("application/json")]
    [Route("api/infrastructure/whitelist")]
    public sealed class WhitelistController : Controller
    {
        private readonly IInfrastructure _infrastructure;
        public WhitelistController(IInfrastructure infrastructure) => _infrastructure = infrastructure;

        [HttpGet]
        [Route("/")]
        public async Task<IEnumerable<WhitelistRequest>> GetWhitelists() => await _infrastructure.GetPendingWhitelistRequests().ConfigureAwait(false);

        [HttpPost]
        [Route("/")]
        public async Task<ActionResult<WhiteListingResponse>> RequestWhitelisting(Guid agentId, string fqdn)
        {
            var result = await _infrastructure.RequestWhitelistingForAgent(agentId, fqdn).ConfigureAwait(false);
            switch (result)
            {
                case WhiteListingResponse.Blacklisted:
                    return Forbid();
                case WhiteListingResponse.SuccessfullyAddedToAwaitingWhitelist:
                    return Ok();
                case WhiteListingResponse.ConflictStopsAddingToAwaitingWhitelist:
                {
                    await _infrastructure.CreateUnresolvedIdentity(agentId, fqdn).ConfigureAwait(false);
                    return Conflict();
                }
                default:
                   return BadRequest();
            }
        }
   }

为什么不是disambiguating基于Route attribute控制器上的方法?

标签: c#asp.net-mvcasp.net-core-mvc

解决方案


应用于以 / 开头的操作的路由模板不会与应用于控制器的路由模板组合。

[Route("Home")]
public class HomeController : Controller
{
    [Route("")]      // Combines to define the route template "Home"
    [Route("Index")] // Combines to define the route template "Home/Index"
    [Route("/")]     // Doesn't combine, defines the route template ""
    public IActionResult Index()
    {
        ViewData["Message"] = "Home index";
        var url = Url.Action("Index", "Home");
        ViewData["Message"] = "Home index" + "var url = Url.Action; =  " + url;
        return View();
    }

    [Route("About")] // Combines to define the route template "Home/About"
    public IActionResult About()
    {
        return View();
    }   
}

这是设计使然,并在此处记录。


推荐阅读