Giter Site home page Giter Site logo

odata-8-example's Introduction

Bicycle Store (Simple OData Example)

Simplest approach to create OData controller and run queries with latest OData 8.x in .NET 5 and .NET 6.

I couldn't find bare bones example, so here it is.

How to use OData 8

Add OData package

<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.0-rc" />

Configure OData service for WebHost

services.AddOData(opt =>
{
    // specify EDM with "odata" prefix path
    opt.AddModel("odata", EdmModelBuilder.GetEdmModel();
    // enable features for OData 
    opt.Filter().Select().OrderBy().SetMaxTop(50).Count();
});

Configure EDM (Entity Data Model)

EDM is used to describe exposed data in the OData (resources, their properties and relations).
Each resource should have key defined. By default, property with name Id is taken, but one can use [Key] attribute with other property. Check Bicycle model for example.

This is sample setup of EDM:

internal static IEdmModel GetEdmModel()
{
    var builder = new ODataConventionModelBuilder();
    // "Bicycles" will be used as a OData resource path
    builder.EntitySet<Bicycle>("Bicycles");

    return builder.GetEdmModel();
}

Create Controller and endpoint

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

public class BicyclesController : ODataController
{ 
    // enable OData queries for this endpoint
    [EnableQuery()]
    public ActionResult<IEnumerable<Bicycle>> Get()
    {
        return Ok(_bicycleRepository.GetBicycles());
    }
}

Example queries

Get top 5 lightest bikes
/odata/Bicycles?$top=5&$orderby=Weight

Get Red bicycles only
/odata/Bicycles?$filter=Color eq 'Red'

Get bicycles lighter or equal 9 (kg)
/odata/Bicycles?$filter=Weight le 9

Resources

odata-8-example's People

Contributors

mrzeznik avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.