By Serge


2018-09-14 15:23:56 8 Comments

I try to use OData in an ASP. Bellow my code:

//============== Startup.cs
public void ConfigureServices(IServiceCollection services) {
    services.AddDbContext<EntriesContext>(
        opt => opt.UseMySql("server=localhost;database=mydb;user=myusr;password=mypass",
        mysqlOptions =>{mysqlOptions.ServerVersion(new Version(5..), ServerType.MySql);}));

    services.AddOData();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        ...
        app.UseMvc(b => { b.MapODataServiceRoute("odata", "odata", GetEdmModel()); });
    }

    private static IEdmModel GetEdmModel() {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Entry>("Entries");
        return builder.GetEdmModel();
    }

The Controller:

[Route("api/[controller]")]
[ApiController]
[EnableCors("AllowMyOrigin")]
public class EntriesController : ODataController
{
    private readonly EntriesContext _context;

    public EntriesController(EntriesContext context) {
        _context = context;
    }

    [HttpGet]
    [EnableQuery]
    public ActionResult<List<Entry>> GetAll() {
        return _context.Entries.ToList();
    }

And the context:

public class EntriesContext : DbContext
{
    public EntriesContext(DbContextOptions<EntriesContext> options) : base(options) { }
    public DbSet<Entry> Entries { get; set; }
}

however, is not clear for me, what path should I use to get the entries (without OData I would use localhost:9000/api/entries, but now I am confused).

I tried to do https://localhost:44384/odata/entries and https://localhost:44384/odata/api/entries but I get 404

I tried to comment the controller's route, like this

//[Route("api/[controller]")]
//[ApiController]
[EnableCors("AllowMyOrigin")]
public class EntriesController : ODataController 

and also modified the action

[HttpGet]
[EnableQuery]
public IActionResult Get() {
    return Ok(_db.Entries);
}

I tried then https://localhost:44384/odata/Entries and gotthe full list of the entries... However, the https://localhost:44384/odata/Entries?$take=2 does not work: 400 Bad Request: Parameter name: $take' is not supported."

1 comments

@Alex Riabov 2018-09-14 15:57:29

You don't need to use [ApiController] annotation when you inherit from ODataController. Remove the annotation and it should work as expected. To be able to query using $take you need to setup it:

app.UseMvc(b =>
      {
        b.Take();
        b.MapODataServiceRoute("odata", "odata", GetEdmModel());
      });

You can also add other actions, e.g. Select().Expand().Filter().OrderBy().MaxTop(100).Count()

@Serge 2018-09-14 16:02:07

I also remarked it does not work with public ActionResult<List<Entry>> GetAll() { but with IActionResult Get()

@Serge 2018-09-14 16:08:03

Now I get "Parameter name: $take' is not supported" when try to use skip or take (see my edit)

@Alex Riabov 2018-09-14 16:15:01

@Serge updated answer

@Serge 2018-09-14 17:10:02

Take such method doesn't exist... I did b.Select().Expand().Filter().OrderBy().MaxTop(100).Count();

@Serge 2018-09-14 17:19:02

take is not supported by the ASP.NET Core....

@Serge 2018-09-14 17:20:45

but we can use $ top instead...

Related Questions

Sponsored Content

0 Answered Questions

Adding a custom property to an OData Child Collection

2 Answered Questions

6 Answered Questions

29 Answered Questions

[SOLVED] How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

2 Answered Questions

[SOLVED] Issue with serving some static files within ASP.NET Core MVC

1 Answered Questions

OData filter with "and" operator not working in Web Api

2 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] All OData requests to C# WebAPI fail

1 Answered Questions

use $inlinecount without MapODataServiceRoute

Sponsored Content