2
我使用.NET的核心2.0.0 https://www.nuget.org/packages/microsoft.aspnetcore.odata的OData在列表.NET的核心Web API序列化/字典
這是我的設置爲止。
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());
});
...
}
控制器
[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>();
}
模型
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; }
}
的問題是,與services.AddOData();
我只得到屬性Id的返回結果,沒有services.AddOData();
我得到了json格式的所有屬性。
我該如何序列化列表屬性,也許詞典以及?
謝謝。
我發表您的問題https://github.com/joshcomley/Microsoft.AspNetCore.OData/issues/2 –
@SamXu謝謝指點:)先生。 –