ASP.NET Core Attribute routing

Asked

Viewed 544 times

0

I’m migrating a project to ASP.NET Core 2.0, but I’m unable 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
    }  
}
  • I had the same problem some time ago, in my case it was a route pointing to two different methods in different controllers

  • In my case, I only have one controller, and it’s conflicting with public Iactionresult Get([Fromquery] Paginationhelper pagination) e public Iactionresult Getme(bool me)

2 answers

2

I think I’ve located your problem... try to put an httpAtribut to your method, because even the two expected to receive different things the route system cannot understand for which 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);
        }
    }

Doesn’t 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 he does not know which method to satisfy then 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 helped, let me know if it doesn’t work!

  • thanks for the help, but it does not solve my case, this parameter me, serves as a filter, in this same class in WEBAPI2, I have a method //api/Sales? active=true [Actionname("Get")] public Httpresponsemessage Getactive(bool? active) that works the same way as the/Sales api? me .

0

It has two methods with the same number of parameters and the same name so you have to change the name of a

 [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
        }  
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.