C#OData4 WebApi无法扩展导航属性

时间:2017-01-11 00:25:59

标签: c# asp.net-web-api asp.net-web-api2 odata navigation-properties

我正在创建一个实现OData 4的新Web API。我希望能够选择/扩展导航属性,但是我收到一个我不知道如何解决的错误。

我已经发布了请求uri,响应错误,示例代码和下面的堆栈跟踪。如果你看一下代码,“Phones”显然是一种方法,而不是像Id和Name这样的属性,但我认为我应该能够选择和扩展该方法的结果,就像属性一样。

如何允许API用户选择从该方法返回的值?

P.S。导航属性可能不是正确的名称(?)

请求URI

http://localhost:10013/Call?$select=*&$expand=Phones

RESPONSE

{
    "error" : {
        "code" : "",
        "message" : "The query specified in the URI is not valid. Could not find a property named 'Phones' on type 'Call'.",
        "innererror" : {
            "message" : "Could not find a property named 'Phones' on type 'Call'.",
            "type" : "Microsoft.OData.ODataException"
        }
    }
}

代码

public class CallController : ODataController
{
    [EnableQuery]
    public IQueryable<Call> GetCall(ODataQueryOptions options)
    {
        return DatabaseContext.Call.GetAll();
    }
}
public class Call
{
    public int Id { get; set; }
    public string Name { get; set; }

    public IEnumerable<Phone> Phones()
    {
        //just an example
        var list = new List<Phone>();
        return list as IEnumerable<Phone>
    }
}
public class Phone
{
    public int Id { get; set; }
    public string Name { get; set; }
}

STACK TRACE

  1. Microsoft.OData.UriParser.SelectExpandBinder.GenerateExpandItem(ExpandTermToken tokenIn)
  2. System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
  3. System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
  4. System.Collections.Generic.List 1.InsertRange(Int32 index, IEnumerable 1个集合)
  5. Microsoft.OData.UriParser.SelectExpandBinder.Bind(ExpandToken tokenIn)
  6. Microsoft.OData.UriParser.SelectExpandSemanticBinder.Bind(ODataPathInfo odataPathInfo,ExpandToken expandToken,SelectToken selectToken,ODataUriParserConfiguration配置)
  7. Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(String select,String expand,ODataUriParserConfiguration configuration,ODataPathInfo odataPathInfo) Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()
  8. System.Web.OData.Query.Validators.SelectExpandQueryValidator.Validate(SelectExpandQueryOption selectExpandQueryOption,ODataValidationSettings validationSettings)
  9. System.Web.OData.Query.SelectExpandQueryOption.Validate(ODataValidationSettings validationSettings)
  10. System.Web.OData.Query.Validators.ODataQueryValidator.Validate(ODataQueryOptions选项,ODataValidationSettings validationSettings)
  11. System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)
  12. System.Web.OData.EnableQueryAttribute.ValidateQuery(HttpRequestMessage request,ODataQueryOptions queryOptions)
  13. System.Web.OData.EnableQueryAttribute.ExecuteQuery(Object response,HttpRequestMessage request,HttpActionDescriptor actionDescriptor,ODataQueryContext queryContext)
  14. System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)

1 个答案:

答案 0 :(得分:0)

您似乎在Call类中创建了名为Phones的函数。请将该功能转换为属性,如下所示:

公共IEnumerable手机{get; set;}