2017-05-16 161 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错误?我如何简单地将PathAndQuery添加到我的crmEndPoint并返回结果?

+1

应该不是OData的操作方法使用IQueryable的''作为返回类型? –

回答

1

OData框架在纯Web API的基础上提供额外的响应格式化/查询规则。

使用ODataQueryOptions参数要求该操作方法将返回IQueryable<T>IEnumerable<T>

ODataQueryOptions只是有助于解析传入的OData请求的URL制作参数,如$filter$sort通过属性可访问。

您的代码不需要此服务,因为它只是将请求重定向到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(); 
}