ASP.NET Core Attribute routing

0

I'm migrating a project to ASP.NET Core 2.0, but I'm not able to use the following routes.

Error message.

An unhandled exception occurred while processing the request.

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

ASP.Net WebAPI2

[Route("api/[controller]")]
public class SalesController : Controller
{
    // api/sales/1 -> funciona
    [HttpGet]
    public HttpResponseMessage Get(int id)
    {
        // Logic
    }   

    // api/sales?page=1&pageSize-20 -> funciona
    [HttpGet]
    public HttpResponseMessage Get([FromUri] PaginationHelper pagination)
    {
        // Logic
    }  

    // api/sales?me -> funciona
    [ActionName("Get")]
    public HttpResponseMessage GetMe(bool? me)
    {
        // Logic
    }  
}

ASP.Net Core 2.0

[Route("api/[controller]")]
public class SalesController : Controller
{
    // api/sales/1 -> funciona
    [HttpGet("{id:int}")]
    public IActionResult Get(int id)
    {
        // Logic
    }   

    // api/sales?page=1&pageSize-20 -> não funciona
    [HttpGet]
    public IActionResult Get([FromQuery] PaginationHelper pagination)
    {
        // Logic
    }  

    // api/sales?me=false -> não funciona
    [HttpGet]
    public IActionResult GetMe(bool me)
    {
        // Logic
    }  
}
    
asked by anonymous 27.09.2017 / 03:01

2 answers

2

I think I've located your issue ... try to put an httpAtribut for your method, because even the two expected to receive different things the routes system can not understand for what it is:

works:

 [Route("api/[controller]")]
    public class ValuesController : Controller
    {
        //GET api/values
       [HttpGet]
        public IActionResult Get()
        {
            return Ok("vazio");
        }

        // GET api/values/5
        [HttpGet("{id:int}")]
        public IActionResult Get(int id)
        {
            return Ok(id);
        }
        [HttpGet("{xx}")]// coloque isso --------------------!!!!
        public IActionResult Get([FromQuery] string xx)
        {
            return Ok("funcionou c:");
        }

        // GET api/values/true
        [HttpGet("{me:bool}")]// e isso --------------------!!!!
        public IActionResult Get(bool me)
        {
            return Ok(!me);
        }
    }

Does not work:

public class ValuesController : Controller
{
    //GET api/values
   [HttpGet]
    public IActionResult Get()
    {
        return Ok("vazio");
    }

    // GET api/values/5
    [HttpGet("{id:int}")]
    public IActionResult Get(int id)
    {
        return Ok(id);
    }
    [HttpGet]
    public IActionResult Get([FromQuery] string xx)
    {
        return Ok("não funcionou :c");
    }

    // GET api/values/true
    [HttpGet]
    public IActionResult Get(bool me)
    {
        return Ok(!me);
    }
}

}

The error that returns indicates that it does not know which method to satisfy so it ends up releasing an exeption

An unhandled exception occurred while processing the request.

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

WebApplication1.Controllers.ValuesController.Get (WebApplication1) WebApplication1.Controllers.ValuesController.Get (WebApplication1) Microsoft.AspNetCore.Mvc.Internal.ActionSelector.SelectBestCandidate (RouteContext context, IReadOnlyList candidates)

I hope I have helped, let me know if it does not work!

    
28.09.2017 / 01:25
0
  

It has two methods with the same number of parameters and the   same name then you have to rename one

 [Route("api/[controller]")]
    public class SalesController : Controller
    {
        // api/sales/1 -> funciona
        [HttpGet("{id:int}")]
        public IActionResult Get(int id)
        {
            // Logic
        }   

        // api/sales?page=1&pageSize-20 -> não funciona
        [HttpGet]
        [Route("OutroNome")]  // <--- Tem dois metodos com o mesmo numero de parametros e  o mesmo nome então tem de mudar o nome de um
        public IActionResult Get([FromQuery] PaginationHelper pagination)
        {
            // Logic
        }  

        // api/sales?me=false -> não funciona

        [HttpGet]
        public IActionResult GetMe(bool me)
        {
            // Logic
        }  
    }
    
16.09.2018 / 04:31