简体   繁体   English

用于列表/字典的.NET Core Web API序列化中的OData

[英]OData in .NET Core Web API Serialization for List / Dictionary

I'm using .NET Core 2.0.0 with https://www.nuget.org/packages/microsoft.aspnetcore.odata 我正在使用.NET Core 2.0.0和https://www.nuget.org/packages/microsoft.aspnetcore.odata

This is my setup so far. 这是我到目前为止的设置。

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddOData();
    services.AddSingleton<IODataModelManger, ODataModelManager>(DefineEdmModel);
    ...
}

private ODataModelManager DefineEdmModel(IServiceProvider services)
{
    var modelManager = new ODataModelManager();

    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<TestDTO>(nameof(TestDTO));
    builder.EntityType<TestDTO>().HasKey(ai => ai.Id); // the call to HasKey is mandatory
    modelManager.AddModel(nameof(Something), builder.GetEdmModel());

    return modelManager;
}

public void Configure(...)
{
    ...
    var modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<TestDTO>("TestDTOs");

    app.UseMvc(builder =>
    {
        builder.MapODataRoute("api", modelBuilder.GetEdmModel());
    });
    ...
}

Controller 调节器

[HttpGet("all")]
public async Task<IQueryable<TestDTO>> Get()
{
    // plug your entities source (database or whatever)
    var test = await TestService.GetTest();

    var modelManager = (IODataModelManger)HttpContext.RequestServices.GetService(typeof(IODataModelManger));
    var model = modelManager.GetModel(nameof(Something));
    var queryContext = new ODataQueryContext(model, typeof(TestDTO), null);
    var queryOptions = new ODataQueryOptions(queryContext, HttpContext.Request, Provider);

    return queryOptions
        .ApplyTo(test, new ODataQuerySettings
        {
            HandleNullPropagation = HandleNullPropagationOption.True
        }, null)
        .Cast<TestDTO>();
}

Model 模型

public class TestDTO : BaseEntityDTO
{
    [Required]
    public Guid Id { get; set; }
    public List<CustomerDTO> Customers { get; set; }
    public List<string> Tags { get; set; }
    [JsonExtensionData]
    public IDictionary<string, object> AddProperties { get; set; }
}

The problem is that with services.AddOData(); 问题是使用services.AddOData(); i only get return results with property Id, without services.AddOData(); 我只获得带有属性Id的返回结果,没有services.AddOData(); I get all the properties json formatted. 我得到json格式化的所有属性。

How do I serialize also the List properties and maybe the Dictionary aswell? 如何序列化List属性,也可能是字典?

Thanks. 谢谢。

This did the trick 这样做了

var result = queryOptions
                         .ApplyTo(test, new ODataQuerySettings
                         {
                             HandleNullPropagation = 
                             HandleNullPropagationOption.True
                          }, null)
                          .Cast<TestDTO>();
return Json(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM