Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core Attribute routing

I am migrating a project to asp net core, but I could not configure these routes, i am using attributes to map actions.

Code for ASP.Net WebAPI2

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

    // api/sales -> ok
    [HttpGet]
    public HttpResponseMessage Get([FromUri] PaginationHelper pagination)
    {
        // Logic
    }  

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

Code for ASP.Net Core

In the file Startup.cs is set app.UseMvc();

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

    // api/sales -> don't work
    [HttpGet] // -> ???
    public IActionResult Get(PaginationHelper pagination)
    {
        // Logic
    }  

    // api/sales?me -> don't work
    [HttpGet] // -> ???
    public IActionResult GetMe(bool? me)
    {
        // Logic
    }  
}
like image 923
Willian Teleginski Avatar asked Sep 26 '17 12:09

Willian Teleginski


2 Answers

Maybe I'm too late to this discussion, but this could be valuable to other people who end up reading this page, like I did. The problem with your API endpoints is that you configured 2 of them to handle the same route: GET /api/sales.

If you try to access that route, the framework won't be able to distinguish between Get(PaginationHelper) and GetMe(bool?). The reason why is that they both have [HttpGet] attribute, which means that both are capable of handling the route you specified in the [Route("api/[controller]")] attribute, just above your class declaration. Since those are declared as capable of handling that same route, hence your Exception (Multiple actions matched).

The solution for your problem depends on which action you want to handle that ambiguous route. Assuming that you want the route GET /api/sales to be handled by Get(PaginationHelper), you can change the GetMe(bool?) action method and its [HttpGet] attribute to something like this:

[HttpGet("me")] // GET api/sales/me
public IActionResult GetMe() {
    // Logic
}
like image 140
Mladen B. Avatar answered Oct 16 '22 18:10

Mladen B.


The new framework expects a more explicit indication of the intent of the endpoint.

[Route("api/[controller]")]
public class SalesController : Controller {

    [HttpGet("{id:int}")] // GET api/sales/1
    public IActionResult Get(int id) {
        // Logic
    }

    [HttpGet] // GET api/sales?page=1 assuming PaginationHelper has page property
    public IActionResult Get([FromQuery]PaginationHelper pagination) {
        // Logic
    } 

    [HttpGet] // GET api/sales?me=true
    public IActionResult GetMe(bool? me = false) {
        // Logic
    }  
}

Reference Asp.Net Core: Model Binding

like image 33
Nkosi Avatar answered Oct 16 '22 18:10

Nkosi