locked
EF web api get method query RRS feed

  • Question

  • User2096382003 posted

    I have scaffolded my models from an existing database then created an api controller from my model and context.

    public partial class Order
        {
            public Order()
            {
                OrderItem = new HashSet<OrderItem>();
            }
    
            public int Id { get; set; }
            public string OrderNo { get; set; }
            public int CustomerId { get; set; }
            public string Notes { get; set; }
            public DateTimeOffset Created { get; set; }
            public DateTimeOffset Due { get; set; }
            public int PriorityId { get; set; }
            public byte[] Rowversion { get; set; }
    
            public virtual Customer Customer { get; set; }
            public virtual LutPriority Priority { get; set; }
            public virtual ICollection<OrderItem> OrderItem { get; set; }
        }

    the default get method retrieves an order by id which is fine, but i want to retrieve order(s) via other means such as by customerid, date range etc...

    // GET: api/Orders/5
            [HttpGet("{id}")]
            public async Task<ActionResult<Order>> GetOrder(int id)
            {
                var order = await _context.Order.FindAsync(id);
    
                if (order == null)
                {
                    return NotFound();
                }
    
                return order;
            }
    
            // GET: api/OrdersByCustomer/5
            [HttpGet("{customerid}")]
            public async Task<ActionResult<Order>> GetOrdersByCustomerId(int customerid)
            {
                var order2 = await _context.Order.FindAsync(customerid);
    
                if (order2 == null)
                {
                    return NotFound();
                }
    
                return order2;
            }

    how do i add these get methods? i tried using a copy of the id method with customerid but get an error

    An unhandled exception occurred while processing the request.

    AmbiguousMatchException: The request matched multiple endpoints. Matches:

    WebApi.Controllers.OrdersController.GetOrdersByCustomerId (WebApi)
    WebApi.Controllers.OrdersController.GetOrder (WebApi)
    Wednesday, July 15, 2020 9:32 PM

All replies

  • User475983607 posted

    In REST (Web API) the HTTP method is used to select the action not the action name.  If you want to add the action name then either update the default route to include the action or add an attribute route. https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1

        [Route("api/[controller]")]
        [ApiController]
        public class DefaultController : ControllerBase
        {
            private readonly ILogger<WeatherForecastController> _logger;
            public DefaultController(ILogger<WeatherForecastController> logger)
            {
                _logger = logger;
            }
    
            //GET: api/default/1
            [HttpGet("{id}")]
            public ActionResult<string> GetOrder(int id)
            {
                return $"Hello from GetOrder {id}";
            }
    
            //GET: api/default/GetOrdersByCustomerId/1
            [HttpGet("GetOrdersByCustomerId/{customerid}")]
            public ActionResult<string> GetOrdersByCustomerId(int customerid)
            {
                return $"Hello from GetOrdersByCustomerId {customerid}";
            }
       }

    URL

    https://localhost:44379/api/default/1

    Result

    Hello from GetOrder 1

    URL

    https://localhost:44379/api/default/GetOrdersByCustomerId/1

    Result

    Hello from GetOrdersByCustomerId 1

    Wednesday, July 15, 2020 10:33 PM