Web API不返回单个记录,而是返回列表。 MVC 4

时间:2015-08-25 17:02:01

标签: asp.net-mvc web asp.net-web-api asp.net-web-api-routing

My Web API有两个连接到存储库的方法。 当我打电话给

"api/Cust/GetCustomers" 

正在返回我的数据库中的完整客户列表。这可以。作为抬头,我正在使用Northwind,因此客户的ID是一组字母。例如 - ALFKI或ANTON

当我拨打特定的CustomerID时,例如

"api/Cust/GetCustomers/alfki"

我没有收到错误,但返回了上面的相同列表(包含数据库中的所有客户)。我发现这很奇怪,因为我的印象是如果我的控制器或存储库中的某些内容不正确,我会收到一个未找到的错误。

有经验的人是否知道这样的事情发生了。 我已经完成了一个已经完成的示例,在该示例中,导航到特定的将只返回该客户的记录,这正是我想要做的。

这是我的api控制器中的代码,几乎完全相同 我认为在路由配置中必须有一些微妙的东西可能导致这种情况而不会导致错误

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]
    [Route("api/Cust/GetCustomers/alfki")]
    public Customer GetCustomerByID(string id)
    {
        Customer customer = _repo.GetCustomerByID(id);
        return customer;
    }


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



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



}

这是我的回购

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();

    }

以下链接指向我的示例中的网址截图,按预期工作并返回特定ID的记录 http://postimg.org/image/oup88k83f/

在第二个中,它是我的api的一个链接,我一直在我的榜样上工作。 http://postimg.org/image/858t1oph9/

如上所述,除了对路由的一些小改动以及api控制器名称之外,代码几乎相同。

如果有人知道造成这种情况的原因,所有建议都表示赞赏。 谢谢

*更新修复了我的代码中的拼写错误

我的routeconfig.cs(与模板相同,在创建新项目时提供了我的MVC4 API选择)

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

0 个答案:

没有答案