What is OData?
OData also provides guidance for tracking changes, defining functions/actions for reusable procedures, and sending asynchronous/batch requests.
When should I use OData ?
OData Example implementation
- Microsoft.AspNetCore.Mvc.NewtonsoftJson Version 7.0.2
- Microsoft.AspNetCore.OData Version 7.5.5
[Route("api/[controller]")]
[ApiController]
public class StudentsController : ControllerBase
{
[HttpGet]
[EnableQuery]
public IEnumerable GetStudents() => new List()
{
new Student(1,"Josh","McCall",new DateTime(1990,7,15),true,95),
new Student(2,"Kailu","Hu",new DateTime(1984,7,27),false,60),
new Student(3,"Mayra","Stephenson",new DateTime(1977,1,1),true,87),
new Student(4,"Winston","Smith",new DateTime(1984,1,27),true,100),
new Student(5,"Sophia","Willams",new DateTime(2000,2,8),false,99),
new Student(6,"Jacob","Mason",new DateTime(2002,1,2),true,55),
new Student(7,"Olivia","Macklemore",new DateTime(2001,9,11),false,23)
};
}
namespace ODataWithNet5Demo.Models
{
public record Student(int Id,
string FirstName,
string LastName,
DateTime BirthDate,
bool IsAdvanced,
int Grade);
}
Next, we need to register OData services in the ConfigureServices startup method.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
services.AddOData();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "odata demo", Version = "v1" });
});
}
Also finally add this to the Startup Configure method.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "odata v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.EnableDependencyInjection();
endpoints.Select().Filter().Expand().Count().OrderBy().MaxTop(100);
});
}
This is the bare minimum we need to start making requests using OData.
If you go to the browser you can query: https://localhost:44319/api/students?$filter=grade gt 90
What if I need to use $select?
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
services.AddOData();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "odata", Version = "v1" });
});
}
Then, if you go to the browser you can query https://localhost:44319/api/students?$select=firstname

Does OData support pagination?
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ODataWithNet5Demo v1"));
}
app.UseHttpsRedirection();
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Filter().Expand().MaxTop(100).Count().OrderBy();
routeBuilder.EnableDependencyInjection();
routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet("Students");
return builder.GetEdmModel();
Then if you go to the browser, you can query https://localhost:44319/odata/students?$select=firstname&$count=true&$top=3

Notice that the url is not /api/students , but /odata/students. In this case we are using the url configured in the OData Service Route. This url gives us access to OData metadata, such as the context and the count, as we can see from the previous image. We can use the count value to configure paginators in the frontend in the event of having a lot of rows to display. In this example, we are also using the $top operator which we can use as the page size.
Note: Make sure to name the action method in the Controller GetStudents() (Get[EntityName]) so that the OData EDM Model can find it. If you name the action method something like GetAllStudents() you will get an error. You can take a look at the source code here.

About the Author
Javier Acrich a multi-talented .NET Developer capable of managing both front-end and back-end development. Successfully creates and modifies web features to improve functioning for clients. Skilled in architecture and design of web applications and solutions.