Skip to content

Asp .net core

Maxim edited this page Nov 14, 2019 · 4 revisions

Client Microsoft.OData.Client - test\OdataToEntity.Test.Asp\OdataToEntity.Test.AspClient.
Server Asp .net core - test\OdataToEntity.Test.Asp\OdataToEntity.Test.AspServer.
Server Asp mvc .net core - test\OdataToEntity.Test.Asp\OdataToEntity.Test.AspMvcServer.

public void ConfigureServices(IServiceCollection services)
{
    var dataAdapter = new OrderDataAdapter(true, true);
    services.AddOdataToEntityMvc(dataAdapter.BuildEdmModelFromEfCoreModel());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRouting();
    app.UseEndpoints(b => b.MapControllers());
}
[Route("api/[controller]")]
public sealed class OrdersController
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public OrdersController(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }
    [HttpGet]
    public async Task<ODataResult<Model.Order>> Get()
    {
        var parser = new OeAspQueryParser(_httpContextAccessor.HttpContext);
        Model.OrderContext orderContext = parser.GetDbContext<Model.OrderContext>();
        IAsyncEnumerable<Model.Order> orders = parser.ExecuteReader<Model.Order>(orderContext.Orders.AsQueryable().Where(o => o.Id > 0));
        List<Model.Order> orderList = await orders.OrderBy(o => o.Id).ToListAsync();
        return parser.OData(orderList);
    }
}