我正在创建一个新的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; }
}
堆叠跟踪
- Microsoft.OData.UriParser.SelectExpandBinder.GenerateExpandItem(ExpandTermToken tokenIn)
- System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
- System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
- System.Collections.Generic.List
1.InsertRange(Int32 index, IEnumerable
1集合) - Microsoft.OData.UriParser.SelectExpandBinder.Bind(ExpandToken标记In)
- Microsoft.OData.UriParser.SelectExpandSemanticBinder.Bind(ODataPathInfo ODataPathInfo、ExpandToken ExpandToken、SelectToken SelectToken、ODataUriParserConfiguration配置)
- Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpandImplementation(字符串选择,字符串扩展,ODataUriParserConfiguration配置,ODataPathInfo ODataPathInfo)Microsoft.OData.UriParser.ODataQueryOptionParser.ParseSelectAndExpand()
- System.Web.OData.Query.Validators.SelectExpandQueryValidator.Valide(SelectExpandQuery Option SelectExpandQueryOption,ODataValidationSettings validationSettings)
- System.Web.OData.Query.SelectExpandQueryOption.Valide(ODataValidationSettings validationSettings)
- System.Web.OData.Query.Validators.ODataQueryValidator.Valide(ODataQueryOptions选项,ODataValidationSettings validationSettings)
- System.Web.OData.Query.ODataQueryOptions.Validate(ODataValidationSettings validationSettings)
- System.Web.OData.EnableQueryAttribute.ValideQuery(HttpRequestMessage请求,ODataQueryOptions queryOptions)
- System.Web.OData.EnableQueryAttribute.ExecuteQuery(对象响应、HttpRequestMessage请求、HttpActionDescriptor actionDescriptor、ODataQueryContext queryContext)
- System.Web.OData.EnableQueryAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedCntext)
您似乎已经在类Call中创建了名为Phones的函数。请将该函数转换为如下属性:
公用IEnumerable电话{get;set;}