mardi 25 août 2015

Web API NOT returning single record, instead returning list. MVC 4

My Web API has two methods hooked up to a repository. When I make a call to

"api/customersapi" 

the full list of customers in my database is being returned. This is fine. As a heads up, i'm using Northwind so the IDs for a Customer are a group of letters. eg - ALFKI or ANTON

When I make a call to a specific CustomerID, for example

"api/customersapi/alfki"

I don't get an error, but the same list from above(containing all customers in the database) is returned. I'm finding this strange because my impression would be that i'd get a not found error if something is incorrect in my controller or repository.

Does anybody with experience know how something like this happens. I have an already completed example to work off of, and in that example navigating to a specific will return records only for that customer, which is what i'm looking to do.

Here is the code in my api controller, which is almost identical I'm thinking there must be something subtle in the routing configs that could cause this without causing an error

CustomersAPIController.cs

public class CustomersAPIController : ApiController
{
    //
    // GET: /CustomersAPI/

    private INorthwindRepository _repo;

    public CustomersAPIController(INorthwindRepository repo)
    {
        _repo = repo;
    }

    //This routing doesn't work, but if it is a possible issue,
    the call for a specific customer wasn't working before I added it
    [Route("api/Cust/GetOrders({id})")]
    public IQueryable<Order> GetOrdersForCustID(string id)
    {
        return _repo.GetOrdersForCustID(id);
    }


    [Route("api/Cust/GetCustomers")]
    public IQueryable<Customer> GetAllCustomers()
    {
        return _repo.GetCustomers();
    }

    [HttpGet]
    public Customer GetCustomerByID(string id)
    {
        Customer customer = _repo.GetCustomerByID(id);
        return customer;
    }


    //===========================================



    protected override void Dispose(bool disposing)
    {
        _repo.Dispose();
        base.Dispose(disposing);
    }



}

and here is my repo

repo.cs

public interface INorthwindRepository:IDisposable
{
    //private northwndEntities _ctx = new northwndEntities();


    IQueryable<Customer> GetCustomers();
    IQueryable<Customer> TakeTenCustomers();
    Customer GetCustomerByID(string id);

    IQueryable<Order> GetOrders();
    IQueryable<Order> GetOrdersForCustID(string id);
    Order FetchOrderByID(int orderID);
}

public class NorthwindRepository : INorthwindRepository
{

     northwndEntities _ctx = new northwndEntities();


    public IQueryable<Customer> GetCustomers()
    {
        return _ctx.Customers.OrderBy(c => c.CustomerID);
    }

    public IQueryable<Customer> TakeTenCustomers()
    {
        var foo = (from t in _ctx.Customers
                   select t).Take(10);
        return foo;

    }

    public IQueryable<Order> GetOrdersForCustID(string id)
    {

        var orders = _ctx.Orders.Where(x => x.CustomerID == id).OrderByDescending(x=>x.OrderDate).Take(4);


        return orders;

    }

    public Customer GetCustomerByID(string id)
    {
        return _ctx.Customers.Find(id);
    }

    public void Dispose()
    {
        _ctx.Dispose();

    }

Here is a link to a screenshot of the url in my example to work off of, working as intended and returning the records for a specific ID http://ift.tt/1ETLYSM

In this second one, it is a link to my api that I have been basing on my example to work from. http://ift.tt/1EhSacu

As mentioned above, the code is nearly identical, except for some small changes to the routing and maybe the api controller names.

If anyone has any idea what is causing this, all suggestions are appreciated. Thank you




Aucun commentaire:

Enregistrer un commentaire