Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData on .Net Core doesn't return the right results on $select

I've added OData to my WebAPI project.

Versions:

  1. Core 3.1
  2. OData 7.3.0 (beta version in order to work with Core 3.x)
  3. EF Core 3.1.0

Here is my startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<Models.Contexts.EntityContext>(opts => opts.UseSqlServer(Configuration["ConnectionString:MailBackup"]));
        services.AddControllers();
        services.AddMvc(options =>
        {
            options.EnableEndpointRouting = false;
        }).SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);

        services.AddOData();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc(routeBuilder =>
        {
            routeBuilder.EnableDependencyInjection();
            routeBuilder.Expand().Select().OrderBy().Filter();
        });

        app.UseHttpsRedirection();
        app.UseAuthorization();

    }
}

And the controller is:

[EnableQuery()]
[HttpGet]
[Route("GetAll")]
public IQueryable<Models.EmailMessage> GetAll()
{    
    return this._context.EmailMessages;
}

The APIs are working fine, but when I try to add some OData action like $select I get the following and not the expected results:

{
    "instance": null,
    "container": {},
    "modelID": "529e8054-04c4-4729-aa91-d7eaf67a55d0",
    "untypedInstance": null,
    "instanceType": null,
    "useInstanceForProperties": false
},
{
    "instance": null,
    "container": {},
    "modelID": "529e8054-04c4-4729-aa91-d7eaf67a55d0",
    "untypedInstance": null,
    "instanceType": null,
    "useInstanceForProperties": false
},
like image 948
Giox Avatar asked Dec 17 '19 20:12

Giox


People also ask

What is OData in .NET core?

OData stands for Open Data Protocol which helps to build and consuming of RESTFul APIs. It is an ISO/IEC-approved and OASIS standard. OData will take care of various approaches to RESTful API like Status codes, URL conventions, request and response headers, media types, query options, payload formats, etc.

How do I enable OData in Web API?

In Solution Explorer, right click on Models folder > Add > Class > Name your class. Now, we are going to create a Controller. Right click on the Controllers folder > Add > Controller> selecting Web API 2 OData v3 Controller with actions, using Entity Framework > click Add.


1 Answers

I had the same problem. This solved the problem for me. Perhaps odata is not fully compatible with the new JSON serializer in Asp.Net 3.1. I don't know.

services.AddControllers(mvcOptions =>
           mvcOptions.EnableEndpointRouting = false)
       .AddNewtonsoftJson();
like image 175
Steffen Avatar answered Sep 28 '22 02:09

Steffen