0

내 웹 API 방법 중 하나가 완벽하게 작동하며 다른 하나는 전혀 작동하지 않습니다.한 웹 API 방법은 작동하지만 다른 웹 API 방법은 작동하지 않는 이유는 무엇입니까?

enter image description here

다른 하나하지만, 심지어 자체에 대해 알고하지 않는 것 : 작품으로

완벽하게,이 말은. 그것은 함께 브라우저 요청에 응답 :

enter image description here

코드는 둘 다에 대해 동일한 설정 것 같다을, 그래서 하나가 마법처럼 작동하고 다른 하나는 너무 thuddily 실패 이유를 모르겠어요.

관련 코드는 다음과 같습니다

http://shannon2:28642/api/Departments/Count 

브라우저에서 컨트롤러의 GETC 실행하기 위해 노력 :

CONTROLLER

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

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}")] 

수에 따라해야 할 것은 웹 API에서 라우팅 및 행동 선택에 대해 읽을 수있는 좋은 기사입니다 :
http://www.asp.net/web-api/overview/web-api-routing-and-actions

+0

감사합니다; 나는 * 전에 그것을 말했을 수도 있지만, 나쁜 기억이 아니었다면 나는 기억이 전혀 없다. (기억할 수는 없다). –