C#OData4 WebApi在扩展导航属性时失败



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

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

我可以做些什么来允许API的用户选择从该方法返回的值?

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

请求URI

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

响应

{
"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; }
}

堆叠跟踪

  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.List1.InsertRange(Int32 index, IEnumerable1集合)
  5. Microsoft.OData.UriParser.SelectExpandBinder.Bind(ExpandToken标记In)
  6. Microsoft.OData.UriParser.SelectExpandSemanticBinder.Bind(ODataPathInfo ODataPathInfo、ExpandToken ExpandToken、SelectToken SelectToken、ODataUriParserConfiguration配置)
  7. Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(字符串选择,字符串扩展,ODataUriParserConfiguration配置,ODataPathInfo ODataPathInfo)Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()
  8. System.Web.OData.Query.Validators.SelectExpandQueryValidator.Valide(SelectExpandQuery Option SelectExpandQueryOption,ODataValidationSettings validationSettings)
  9. System.Web.OData.Query.SelectExpandQueryOption.Valide(ODataValidationSettings validationSettings)
  10. System.Web.OData.Query.Validators.ODataQueryValidator.Valide(ODataQueryOptions选项,ODataValidationSettings validationSettings)
  11. System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)
  12. System.Web.OData.EnableQueryAttribute.ValideQuery(HttpRequestMessage请求,ODataQueryOptions queryOptions)
  13. System.Web.OData.EnableQueryAttribute.ExecuteQuery(对象响应、HttpRequestMessage请求、HttpActionDescriptor actionDescriptor、ODataQueryContext queryContext)
  14. System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedCntext)

您似乎已经在类Call中创建了名为Phones的函数。请将该函数转换为如下属性:

公用IEnumerable电话{get;set;}

相关内容

  • 没有找到相关文章

最新更新