Component | Build | Status |
---|---|---|
ASP.NET Core OData | Rolling | |
ASP.NET Core OData | Nightly | |
.NET Foundation | Release |
Using .NET CLI:
dotnet add package Microsoft.AspNetCore.OData
Using Package Manager:
Install-Package Microsoft.AspNetCore.OData
Here's a simple example of how to create an OData service using Microsoft.AspNetCore.OData
:
I. Create an ASP.NET Core Application:
- Open Visual Studio and create a new ASP.NET Core Web API project.
II. Add the Microsoft.AspNetCore.OData
Package:
- Install the package using the instructions above.
III. Define Your Models:
- Create your data models. For example:
namespace MyODataApp.Models { public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } }
IV. Add an OData Controller:
- Create a controller to handle OData requests:
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.OData.Routing.Controllers; using MyODataApp.Models; using System.Collections.Generic; using System.Linq; namespace MyODataApp.Controllers { public class ProductsController : ODataController { private static List<Product> products = new List<Product> { new Product { Id = 1, Name = "Product 1", Price = 10.0M }, new Product { Id = 2, Name = "Product 2", Price = 20.0M } }; [EnableQuery] public IActionResult Get() { return Ok(products); } [EnableQuery] public IActionResult Get(int key) { var product = products.FirstOrDefault(p => p.Id == key); if (product == null) { return NotFound(); } return Ok(product); } } }
V. Configure OData in Startup.cs
:
-
Configure OData routes and services:
-
If you work with
Program.cs
, update as below. Refer to the Getting Started Guide.// using statements var builder = WebApplication.CreateBuilder(args); var modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntityType<Order>(); modelBuilder.EntitySet<Customer>("Customers"); builder.Services.AddControllers().AddOData( options => options.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null).AddRouteComponents( "odata", GetEdmModel())); var app = builder.Build(); // Send "~/$odata" to debug routing if enable the following middleware // app.UseODataRouteDebug(); app.UseRouting(); app.MapControllers(); app.Run(); static IEdmModel GetEdmModel() { var builder = new ODataConventionModelBuilder(); builder.EntitySet<Product>("Products"); return builder.GetEdmModel(); }
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.OData.Edm; using Microsoft.OData.ModelBuilder; namespace MyODataApp { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddOData(opt => opt.AddModel("odata", GetEdmModel()).Filter().Select().Expand().OrderBy().Count().SetMaxTop(100)); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // Send "~/$odata" to debug routing if enable the following middleware // app.UseODataRouteDebug(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.Select().Expand().Filter().OrderBy().Count().MaxTop(100); endpoints.MapODataRoute("odata", "odata", GetEdmModel()); }); } private static IEdmModel GetEdmModel() { var builder = new ODataConventionModelBuilder(); builder.EntitySet<Product>("Products"); return builder.GetEdmModel(); } } }
VI. Run Your Application:
- Start your application and navigate to
/odata/Products
to see your OData service in action.
That's it.
This is the official ASP.NET Core OData repository. ASP.NET Core OData is a server side library built upon ODataLib and ASP.NET Core.
Blogs:
For comprehensive documentation, please refer to the following links:
- ASP.NET Core OData Overview
- Getting Started
- Fundamentals Overview
- Tutorials
- OData Dev Blogs
- OData.org
Example:
-
ODataRoutingSample: ASP.NET Core OData sample project in this repo.
-
~/$odata
gives a static routing table page of the service -
~/swagger
gives a swagger/openapi page -
Append
~/$openapi
to each route gives a raw openapi OData page, for example,~/v1/$openapi
Please go to sample folder see more samples.
-
Solution:
-
- Includes Microsoft.AspNetCore.OData project, Unit Test, E2E Test & Samples
-
AspNetCoreOData.NewtonsoftJson.sln
- Includes Microsoft.AspNetCore.OData.NewtonsoftJson project, Unit Test, E2E Test & Samples
Visual Studio 2022 is required to build the source project in order to support the DateOnly
and TimeOnly
types, which were introduced in .NET 6.
Coming soon.
The symbol package is uploaded to nuget symbol server.
It supports source link debug. Remember to check Enable Source Link support
if you debug using Visual Studio.
The nightly build process will upload NuGet packages for ASP.NET Core OData to:
To connect to webapinightly feed, use this feed URL:
-
https://www.myget.org/F/webapinetcore/api/v3/index.json (Your NuGet V3 feed URL (Visual Studio 2015+)
-
https://www.myget.org/F/webapinetcore/api/v2 Your NuGet V2 feed URL (Visual Studio 2012+)
-
ODataRoutingSample: ASP.NET Core OData sample project in this repo.
-
ASP.NET OData 8.0 Preview for .NET 5: A blog introducing the project.
-
Our docs folder: Our current documentation
Any contributions, feature requests, bugs and issues are welcome.
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter. You can also find these instructions in this repo's SECURITY.md.
- Issues: Report issues on Github issues.
- Questions: Ask questions on Stack Overflow.
- Feedback: Please send mails to odatafeedback@microsoft.com.
- Team blog: Please visit https://devblogs.microsoft.com/odata/ and http://www.odata.org/blog/.
This project has adopted the .NET Foundation Contributor Covenant Code of Conduct. For more information see the Code of Conduct FAQ.
This project is supported by the .NET Foundation.
AspNetCoreOData is a Copyright of © .NET Foundation and other contributors. It is licensed under MIT License