Skip to content

OData/AspNetCoreOData

Repository files navigation

ASP.NET Core OData


Component Build Status
ASP.NET Core OData Rolling Build Status
ASP.NET Core OData Nightly Build Status
.NET Foundation Release Build status

A server-side OData library for ASP.NET Core. Use this project to create OData services on top of ASP.NET Core and ODataLib. It provides routing, query handling, model building, formatters and more.

Quick links


Quick start

Using .NET CLI:

dotnet add package Microsoft.AspNetCore.OData

Example - Configure OData

This simple example shows how to register OData and expose an entity set named Products.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.OData;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;

var builder = WebApplication.CreateBuilder(args);

// Register controllers and OData
builder.Services
    .AddControllers()
    .AddOData(options =>
        options.AddRouteComponents("odata", GetEdmModel())
               .Select().Filter().OrderBy().Expand().Count().SetMaxTop(null)); 

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    // Helpful only during development
    app.UseODataRouteDebug();
}

app.UseRouting();
app.MapControllers();
app.Run();

static IEdmModel GetEdmModel()
{
    var builder = new ODataConventionModelBuilder();
    builder.EntitySet<Product>("Products");
    return builder.GetEdmModel();
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Example controller (ODataController)

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.OData.Query;

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);
    }
}

For more examples (including Minimal API samples) see the sample folder in this repository.


Samples

This repository includes a set of example applications under the sample directory. Notable samples:

  • sample/ODataRoutingSample — a full Web API sample showing routing, $metadata and Swagger integration.
  • sample/ODataMiniApi — examples of Minimal API style OData endpoints.

Run a sample:

cd sample/ODataRoutingSample
dotnet run

Then browse to http://localhost:5000/odata/Products (port may vary) to inspect the service.


Github Repository

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:

Documentation:

For comprehensive documentation, please refer to the following links:

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:


Building, Testing, Debugging and Release

1. Building and Testing in Visual Studio

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.

2. One-click build and test script in command line

Coming soon.

3. Debug

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.

4. Nightly Builds

The nightly build process will upload NuGet packages for ASP.NET Core OData to:

To connect to webapinightly feed, use this feed URL:


Documentation


Community

1. Contribution

Any contributions, feature requests, bugs and issues are welcome.

2. Reporting Security Issues

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.

3. Support


Code of Conduct

This project has adopted the .NET Foundation Contributor Covenant Code of Conduct. For more information see the Code of Conduct FAQ.


.NET Foundation

This project is supported by the .NET Foundation.

AspNetCoreOData is a Copyright of © .NET Foundation and other contributors. It is licensed under MIT License

About

ASP.NET Core OData: A server library built upon ODataLib and ASP.NET Core

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors 52