我的一個Web API方法完美工作,另一個完全不起作用。爲什麼一個Web API方法可以工作,而另一個則不行?
通過作品完美,我的意思是這樣的:
另外一個,不過,似乎不知道關於它自己。它回答了與瀏覽器請求:
的代碼似乎是建立在相同的兩個人,所以我不知道爲什麼一個工程就像一個魅力和其他失敗如此thuddily。
相關的代碼是:
控制器
public class DepartmentsController : ApiController
{
private readonly IDepartmentRepository _deptsRepository;
public DepartmentsController(IDepartmentRepository deptsRepository)
{
if (deptsRepository == null)
{
throw new ArgumentNullException("deptsRepository is null");
}
_deptsRepository = deptsRepository;
}
[Route("api/Departments/Count")]
public int GetCountOfDepartmentRecords()
{
return _deptsRepository.Get();
}
[Route("api/Departments")]
public IEnumerable<Department> GetBatchOfDepartmentsByStartingID(int ID, int CountToFetch)
{
return _deptsRepository.Get(ID, CountToFetch);
}
REPOSITORY
public class DepartmentRepository : IDepartmentRepository
{
private readonly List<Department> departments = new List<Department>();
public DepartmentRepository()
{
using (var conn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Freebo;Password=RunningOnEmpty;Data Source=C:\CDBWin\DATA\CCRDAT42.MDB;Jet OLEDB:System database=C:\CDBWin\Data\nrbq.mdw"))
{
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT td_department_accounts.dept_no, IIF(ISNULL(t_accounts.name),'No Name provided',t_accounts.name) AS name FROM t_accounts INNER JOIN td_department_accounts ON t_accounts.account_no = td_department_accounts.account_no ORDER BY td_department_accounts.dept_no";
cmd.CommandType = CommandType.Text;
conn.Open();
int i = 1;
using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
{
while (oleDbD8aReader != null && oleDbD8aReader.Read())
{
int deptNum = oleDbD8aReader.GetInt16(0);
string deptName = oleDbD8aReader.GetString(1);
Add(new Department { Id = i, AccountId = deptNum, Name = deptName });
i++;
}
}
}
}
}
public int Get()
{
return departments.Count;
}
private Department Get(int ID) // called by Delete()
{
return departments.First(d => d.Id == ID);
}
如果進入:
http://shannon2:28642/api/Departments/Count
在瀏覽器的工作原理來執行控制器的GETC ountOfDepartmentRecords()方法,爲什麼進入:
http://localhost:28642/api/Departments/5/6
(或:
http://localhost:28642/api/Departments/1/5
等)無法正常工作執行控制器的GetBatchOfDepartmentsByStartingID()方法?
感謝;我可能在*之前說過,但如果它不是一個不好的記憶,我根本沒有記憶(*不能確定)。 –