Skip to content

Demo application of my speech 'Add OData Support to Your Asp.Net Core Web Api' at Dotnet Konf İstanbul. http://dotnetkonf.com/

Notifications You must be signed in to change notification settings

suadev/aspnet-core-web-api-using-odata

Repository files navigation

Presentation : https://speakerdeck.com/suadev/add-odata-support-to-asp-dot-net-core-web-api

This repo aims to show that how easily OData can be added to your existing/new Asp.Net Core Web Api to perform all CRUD operations.

This is a simple api that consist of two entities called Product and ProductCategory.

localhost:5000/api/products -> classic rest endpoint

localhost:5000/odata/products -> odata rest endpoint

You can check following sample OData queries and results.

if you like Postman, just import AspNetCore_OData.postman_collection.json file and you will see more sample query in Get folders.

{
    "@odata.context": "http://localhost:5000/odata/$metadata#product_categories",
    "@odata.count": 3,
    "value": [
        {
            "id": "1236a458-0802-4340-bdd4-05859c9aaaad",
            "categoryName": "Headphones"
        },
        {
            "id": "8b726886-e719-413c-a125-939ee5af387d",
            "categoryName": "TV"
        },
        {
            "id": "a65bc1ae-c1c7-4c20-8b3b-4b48490d3fb0",
            "categoryName": "Computers"
        }
    ]
}

 {
    "@odata.context": "http://localhost:5000/odata/$metadata#products",
    "@odata.count": 3,
    "value": [
        {
            "id": "6d42ac79-2b65-460d-9991-22b86ad66fb9",
            "productName": "JBL Tune",
            "categoryId": "1236a458-0802-4340-bdd4-05859c9aaaad",
            "description": "JBL Tune 500BT On-Ear",
            "price": 15,
            "weight": 0.3
        },
        {
            "id": "78a4a7db-073f-4ad9-a157-2a9da0ceae38",
            "productName": "HP Zbook",
            "categoryId": "a65bc1ae-c1c7-4c20-8b3b-4b48490d3fb0",
            "description": "HP Zbook Laptop",
            "price": 2000,
            "weight": 3
        },
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch",
            "categoryId": "8b726886-e719-413c-a125-939ee5af387d",
            "description": "LG 32-Inch 720p LED TV",
            "price": 12000,
            "weight": 60
        }
    ]
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products/$entity",
    "id": "6d42ac79-2b65-460d-9991-22b86ad66fb9",
    "productName": "JBL Tune",
    "categoryId": "1236a458-0802-4340-bdd4-05859c9aaaad",
    "description": "JBL Tune 500BT On-Ear",
    "price": 15,
    "weight": 0.3
}

Filter by Price (price greater than 10000) - http://localhost:5000/odata/products?$filter=price%20gt%2010000

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products",
    "value": [
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch",
            "categoryId": "8b726886-e719-413c-a125-939ee5af387d",
            "description": "LG 32-Inch 720p LED TV",
            "price": 12000,
            "weight": 60
        }
    ]
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products",
    "value": [
        {
            "id": "6d42ac79-2b65-460d-9991-22b86ad66fb9",
            "productName": "JBL Tune",
            "categoryId": "1236a458-0802-4340-bdd4-05859c9aaaad",
            "description": "JBL Tune 500BT On-Ear",
            "price": 15,
            "weight": 0.3
        },
        {
            "id": "78a4a7db-073f-4ad9-a157-2a9da0ceae38",
            "productName": "HP Zbook",
            "categoryId": "a65bc1ae-c1c7-4c20-8b3b-4b48490d3fb0",
            "description": "HP Zbook Laptop",
            "price": 2000,
            "weight": 3
        },
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch",
            "categoryId": "8b726886-e719-413c-a125-939ee5af387d",
            "description": "LG 32-Inch 720p LED TV",
            "price": 12000,
            "weight": 60
        }
    ]
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products",
    "@odata.count": 3,
    "value": [
        {
            "id": "78a4a7db-073f-4ad9-a157-2a9da0ceae38",
            "productName": "HP Zbook",
            "categoryId": "a65bc1ae-c1c7-4c20-8b3b-4b48490d3fb0",
            "description": "HP Zbook Laptop",
            "price": 2000,
            "weight": 3
        },
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch",
            "categoryId": "8b726886-e719-413c-a125-939ee5af387d",
            "description": "LG 32-Inch 720p LED TV",
            "price": 12000,
            "weight": 60
        }
    ]
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products",
    "@odata.count": 3,
    "value": []
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products(id,productName)",
    "value": [
        {
            "id": "6d42ac79-2b65-460d-9991-22b86ad66fb9",
            "productName": "JBL Tune"
        },
        {
            "id": "78a4a7db-073f-4ad9-a157-2a9da0ceae38",
            "productName": "HP Zbook"
        },
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch"
        }
    ]
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products",
    "value": [
        {
            "id": "6d42ac79-2b65-460d-9991-22b86ad66fb9",
            "productName": "JBL Tune",
            "categoryId": "1236a458-0802-4340-bdd4-05859c9aaaad",
            "description": "JBL Tune 500BT On-Ear",
            "price": 15,
            "weight": 0.3,
            "category": {
                "id": "1236a458-0802-4340-bdd4-05859c9aaaad",
                "categoryName": "Headphones"
            }
        },
        {
            "id": "78a4a7db-073f-4ad9-a157-2a9da0ceae38",
            "productName": "HP Zbook",
            "categoryId": "a65bc1ae-c1c7-4c20-8b3b-4b48490d3fb0",
            "description": "HP Zbook Laptop",
            "price": 2000,
            "weight": 3,
            "category": {
                "id": "a65bc1ae-c1c7-4c20-8b3b-4b48490d3fb0",
                "categoryName": "Computers"
            }
        },
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch",
            "categoryId": "8b726886-e719-413c-a125-939ee5af387d",
            "description": "LG 32-Inch 720p LED TV",
            "price": 12000,
            "weight": 60,
            "category": {
                "id": "8b726886-e719-413c-a125-939ee5af387d",
                "categoryName": "TV"
            }
        }
    ]
}

{
    "@odata.context": "http://localhost:5000/odata/$metadata#products(id,productName,category(categoryName))",
    "value": [
        {
            "id": "6d42ac79-2b65-460d-9991-22b86ad66fb9",
            "productName": "JBL Tune",
            "category": {
                "categoryName": "Headphones"
            }
        },
        {
            "id": "78a4a7db-073f-4ad9-a157-2a9da0ceae38",
            "productName": "HP Zbook",
            "category": {
                "categoryName": "Computers"
            }
        },
        {
            "id": "aec7ce71-bfa6-4b0f-8aef-78816a31c9fa",
            "productName": "LG 32-Inch",
            "category": {
                "categoryName": "TV"
            }
        }
    ]
}

Running Locally

  • docker-comppose up (for postgres instance)
  • dotnet run

Migration

  • dotnet ef migrations add "init" -c ProductDbContext -p src/OData.WebApi --output-dir Data/Migrations

About

Demo application of my speech 'Add OData Support to Your Asp.Net Core Web Api' at Dotnet Konf İstanbul. http://dotnetkonf.com/

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages