Search code examples
asp.net-coreodata

How to create odata and non-odata endpoint in same controller


Goal: Within the same controller create two httpget endpoints: one for OData and one for non-OData.

This code gives error: AmbiguousMatchException: The request matched multiple endpoints.

[Route("api/products")]
[ApiController]
public class ProductsController : ControllerBase
{

  // OData enabled endpoint
  [EnableQuery]
  [HttpGet]
  public IActionResult GetAllProducts()
  {
    var products = _repo.GetAllProducts();
    return Ok(products);
  }

  // Non-OData enabled endpoint
  [HttpGet]
  public IActionResult GetAllProducts()
  {
    var products = _repo.GetAllProducts().ToList();
    return Ok(products);
  }

Solution

  • Adding the [NonAction] attribute fixed the problem. Is there a better way?

    [EnableQuery]
    [NonAction]
    [HttpGet]
    public IActionResult GetAllProducts()
    {
        var products = _repo.GetAllProducts();
        return Ok(products);
    }