0

我的一個Web API方法完美工作,另一個完全不起作用。爲什麼一個Web API方法可以工作,而另一個則不行?

通過作品完美,我的意思是這樣的:

enter image description here

另外一個,不過,似乎不知道關於它自己。它回答了與瀏覽器請求:

enter image description here

的代碼似乎是建立在相同的兩個人,所以我不知道爲什麼一個工程就像一個魅力和其他失敗如此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()方法?

回答

2

您的航線缺少其參數。

[Route("api/Departments/{ID:int}/{CountToFetch:int}")] 
1

這個問題類似於以下您的其他問題:

Why is my Web API call returning "No action was found on the controller 'DPlatypus' that matches the request"?

如果你期望的值來從URL的非查詢字符串的一部分,你需要定義他們在路由模板中。所以,它應該是

[Route("api/Departments/{id}/{countToFetch}")] 

以下是閱讀有關的Web API路由和行動選擇的好文章:
http://www.asp.net/web-api/overview/web-api-routing-and-actions

+0

感謝;我可能在*之前說過,但如果它不是一個不好的記憶,我根本沒有記憶(*不能確定)。 –

相關問題