Вот рабочая демонстрация, вы можете сослаться на
Установить пакет Microsoft.AspNetCore.OData
-Версия 7.4.0
Модель
public class Student
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Score { get; set; }
}
Атрибут контроллера , EnableQuery
позволяет конечной точке иметь возможности OData
[Route("api/[controller]")]
[ApiController]
public class StudentsController : ControllerBase
{
[HttpGet]
[EnableQuery()]
public IEnumerable<Student> Get()
{
return new List<Student>
{
new Student
{
Id = Guid.NewGuid(),
Name = "Vishwa Goli",
Score = 100
},
new Student
{
Id = Guid.NewGuid(),
Name = "Josh McCall",
Score = 120
}
};
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(mvcOptions =>
mvcOptions.EnableEndpointRouting = false);
services.AddOData();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllers();
//});
app.UseMvc(routeBuilder =>
{
// enable Selection, Expansion, Count, Filter, OrderBy for all routes under “odata/”
routeBuilder.Expand().Select().Count().OrderBy().Filter();
routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel());
});
}
private IEdmModel GetEdmModel()
{
var edmBuilder = new ODataConventionModelBuilder();
edmBuilder.EntitySet<Student>("Students");
return edmBuilder.GetEdmModel();
}
Результат:
Ссылка:
https://devblogs.microsoft.com/odata/experimenting-with-odata-in-asp-net-core-3-1/
https://medium.com/@sddkal / using-odata-controller- IN- net -CORE-АПИС-63b688585eaf