ASP.NET核心路由ambiguousActionException

时间:2018-08-02 10:37:42

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

我有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的方法?

1 个答案:

答案 0 :(得分:2)

应用于/以/开头的动作的路由模板不会与应用于控制器的路由模板结合在一起。

[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();
    }   
}

这是设计使然,并记录在here中。