选择动态属性OData

时间:2019-11-27 21:33:55

标签: c# .net-core odata

我在尝试使用OData 4.0和.net核心选择动态创建的属性时遇到问题。我可以过滤,分组,求和等,但是无法选择字段。这是我的API试图解析的json示例:

[
  {
    "name": "name2",
    "value": 1,
    "details": {
        "field1": 1,
        "field2": "foo"
    }
  },
  {
    "name": "name1",
    "value": 2,
    "details": {
        "field1": 2,
        "field2": "bar"
    }
}]

我尝试了以下语法以仅选择“ field1”:

  • [url] /?$ select =详细信息。得到:“ [”作为回报

  • [url] /?$ select = details / field1 。得到:“在URI中指定的查询无效。不支持选择或扩展查询选项中的路径。”作为回报

  • [url] /?$ expand = details($ select = field1)。得到:“在URI中指定的查询无效。找不到类型为'ProductDiscovery.DTO.GenericResponse'的名为'details'的属性。”作为回报

  • [url] /?$ select = field1 。得到了“在URI中指定的查询无效。不支持选择或扩展查询选项中的路径。”作为回报

使用Microsoft.AspNet.WebApi.OData v5.70和Microsoft.AspNetCore.Odata v7.2.2在.net core 2.2中实现该API。我创建了一个名为“ GenericReponse”的类,该类负责反序列化响应:

public class GenericResponse
{
    [Key]
    public string name { get; set; }
    public int value { get; set; }
    public IDictionary<string, object> details { get; set; }
}

这是我的EdmModel:

        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.ContainerName = "DefaultContainer";

        builder.EntitySet<DTO.GenericResponse>("GenericResponse");
        return builder.GetEdmModel();

也尝试过:

        ODataModelBuilder builder = new ODataModelBuilder();
        ComplexTypeConfiguration<DTO.GenericResponse> genericResponse = 
        builder.ComplexType<DTO.GenericResponse>();
        genericResponse.Property(g => g.name);
        genericResponse.Property(g => g.value);
        genericResponse .HasDynamicProperties(g => g.details);
        return builder.GetEdmModel();

但是得到了相同的结果

那是我的控制器。我现在用json响应模拟两个端点:

    [EnableQuery]
    [HttpGet("filters/odata/queryEnabled/testPolimorfismo")]
    public async Task<IQueryable> BulkRequest8(string port)
    {
        string url1 = @"http://localhost:3000/test"; 
        string url2 = @"http://localhost:4000/test";
        var client = _clientFactory.CreateClient();
        var response1 = client.GetAsync(url1).Result.Content.ReadAsAsync<DTO.GenericResponse>().Result;            
        var response2 = client.GetAsync(url2).Result.Content.ReadAsAsync<DTO.GenericResponse>().Result;
        List<DTO.GenericResponse> responseList = new List<GenericResponse>();
        responseList.Add(response1);
        responseList.Add(response2);
        return responseList.AsQueryable();
    }

是否有解决此错误的建议?

0 个答案:

没有答案