I've added OData to my WebAPI project.
Versions:
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
},
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With