我的Web API有兩個連接到存儲庫的方法。 當我做被返回到Web API不返回單個記錄,而是返回列表。 MVC 4
"api/Cust/GetCustomers"
客戶在我的數據庫的完整名單的電話。這可以。作爲領導,我使用Northwind,因此客戶的ID是一組字母。例如 - ALFKI或ANTON
當我做一個特定的客戶ID的呼叫,例如
"api/Cust/GetCustomers/alfki"
我沒有得到一個錯誤,但是從以上(含所有客戶在數據庫中)同一列表返回。我發現這很奇怪,因爲我的印象是,如果我的控制器或存儲庫中有不正確的東西,我會得到一個未找到的錯誤。
有經驗的人是否知道如何發生這樣的事情。 我有一個已經完成的例子,在這個例子中,導航到一個特定的將只返回該客戶的記錄,這正是我期望做的。
這裏是我的API控制器,它幾乎是相同的代碼 我想必須有路由CONFIGS微妙的東西,可能會導致這不會導致錯誤
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();
}
這裏是我的例子中的URL的截圖鏈接到關的工作,如預期運行和返回記錄特定ID http://postimg.org/image/oup88k83f/
在這第二個,它是我的API鏈接我一直以我的榜樣爲基礎工作。 http://postimg.org/image/858t1oph9/
如上所述,除了對路由和api控制器名稱進行一些小的更改外,代碼幾乎完全相同。
如果有人有任何想法是什麼導致這一點,所有的建議表示讚賞。 謝謝
*更新在我的代碼固定一個錯字
我routeconfig.cs
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 }
);
}
}
您的代碼與您的描述不符。你說你要去'api/customersapi',但你的代碼路由到'api/Cust/GetCustomers',你的屏幕截圖顯示'api/custapi'。你有任何其他代碼指定路線? – Brian
你有'App_Start/RouteConfig.cs'嗎?如果是這樣,請將其添加到您的帖子中。 – Brian
這是一個輸入錯誤 – OisinFoley