2017-05-16 2 views
0

나는 다음과 같은 예외가 점점 오전 :쿼리를 수락하고 비동기 작업을 반환하는 방법?

Cannot create an EDM model as the action 'Get' on controller 'Accounts' has a return type 'System.Web.Http.IHttpActionResult' that does not implement IEnumerable<T>. 

내 엔드 포인트 조회를 시도 : 내가 뭐하는 거지

public async Task<IHttpActionResult> Get(ODataQueryOptions options) 
    { 
     var query = options.Request.RequestUri.PathAndQuery; 
     var client = new HttpClient(); 
     var crmEndPoint = @"HTTPS://MYCRMORG.COM/API/DATA/V8.1/"; 
     HttpResponseMessage response = await client.GetAsync(crmEndPoint+query); 
     object result; 
     if (response.IsSuccessStatusCode) 
     { 
      result = await response.Content.ReadAsAsync<object>(); 

      return Ok(result); 
     } 

     return NotFound(); 
    } 

: 일을하고있다

http://localhost:8267/api/accounts 

AccountsController을 잘못된? 내 crmEndPoint에 PathAndQuery를 추가하고 결과를 반환하려면 어떻게해야합니까?

+1

해야 하는가가 아니라 하나로, OData 액션 메소드를 사용'된 IQueryable '반환 형식으로? –

답변

1

OData 프레임 워크는 일반 웹 API 위에 추가 응답 서식 지정/쿼리 규칙을 제공합니다. ODataQueryOptions 매개 변수 를 사용

는 동작 방법은 IQueryable<T> 또는 IEnumerable<T> 중 하나를 반환이 필요합니다.

ODataQueryOptions은 속성을 통해 $filter$sort과 같은 매개 변수를 만드는 들어오는 OData 요청 URL을 구문 분석하는 데 도움이됩니다.

코드가 모두 crmEndPoint으로 리디렉션되기 때문에 코드에이 서비스가 필요하지 않습니다. 따라서 options.Request을 사용하는 대신 컨트롤러의 Request 속성을 통해 요청 개체에 액세스하고 매개 변수를 모두 삭제할 수 있습니다.

여기에 코드입니다 :

public async Task<IHttpActionResult> Get() 
{ 
    var query = Request.RequestUri.PathAndQuery; 
    var client = new HttpClient(); 
    var crmEndPoint = @"HTTPS://MYCRMORG.COM/API/DATA/V8.1/"; 
    HttpResponseMessage response = await client.GetAsync(crmEndPoint + query); 
    object result; 
    if (response.IsSuccessStatusCode) 
    { 
     result = await response.Content.ReadAsAsync<object>(); 

     return Ok(result); 
    } 

    return NotFound(); 
}