How to fix Odata with C# WebAPI .NET Core Entity Framework

asp.net-core-webapi c# entity-framework-core odata

Question

I can retrieve all of the data but looks like OData is not working I'm using EFCore Entity Framework Web API. This is my reference for OData https://www.youtube.com/watch?v=ZCDWUBOJ5FU

Updated Model below

Return should be

[
    {
        "name":"FakeHelmet"
    },
    {
        "name":"OriginalHelmet"
    },
    {
        "name":"ClassAHelmet"
    }
]

This is my Code.

Controller :

[HttpGet]
[EnableQuery]
public async Task<IActionResult> Get()
{
    var models = await service.Filter();
    return Ok(models);
}

Service

public async Task<IEnumerable<HelmetDto>> Filter()
{
    var models = repo.GetQueryableTest<Helmet>();
    return models.Select(x => SimpleMapper.Map(x, new HelmetDto()));
}

Repository

public virtual IQueryable<TEntity> GetQueryableTest<TEntity>()
where TEntity : class, IEntity
{
    IQueryable<TEntity> query = context.Set<TEntity>();
    return query;
}

I also added this in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
        services.AddOData();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
        app.UseMvc(routeBuilder =>
        {
             routeBuilder.EnableDependencyInjection();        
             routeBuilder.
               Count().
               Filter().
               OrderBy().
               Expand().
               Select().
               MaxTop(null);
         });
}

This is my Model

public class Helmet
{
    public Helmet()
    {
    }

    public int Id { get; set; }

    public String Name { get; set; }

    public String Description { get; set; }

    public String Type { get; set; }
}

the request would be localhost:5002/api/Helmet?$select=name

The OData that I created is not working. Hoping for an answer. This is my reference for OData https://www.youtube.com/watch?v=ZCDWUBOJ5FU

Return should be

[
    {
        "name":"FakeHelmet"
    },
    {
        "name":"OriginalHelmet"
    },
    {
        "name":"ClassAHelmet"
    }
]
1
1
9/11/2019 6:23:43 AM

Popular Answer

It seems that you didn't add routes mapping for OData Service:

Approach 1:

Add a [Route("api/[controller]")] annotation for your HelmetController

[Route("api/[controller]")]
public partial class HelmetController: ControllerBase
{

}

Approach 2

configure a particular OData routes :

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc(routeBuilder =>
    {
         routeBuilder.MapODataServiceRoute(
            "odata",
            "api",
            GetEdmModel(),
            new DefaultODataBatchHandler()
         );
         routeBuilder.EnableDependencyInjection();        
         routeBuilder.
           Count().
           Filter().
           OrderBy().
           Expand().
           Select().
           MaxTop(null);
         routeBuilder.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}"
         );
     });
}

This will make the OData work on route that starts with /api.

Here the GetEdmModel() method is where you setup the EDM model:

private static IEdmModel GetEdmModel()
{
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Helmet>("Helmet")
        .EntityType.Filter().Count().Expand().OrderBy().Page().Select();
}

Note in both situation your controller name should be HelmetController instead of HelmetsController because you're configuring the Model name as Helmet instead of Helmets.

Finally, you can query the Helmet as below :

/api/Helmet?$select=name
0
9/11/2019 7:38:00 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow