Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Add OData Support to Asp.Net Core Web Api

Suat Köse
November 02, 2019

Add OData Support to Asp.Net Core Web Api

Suat Köse

November 02, 2019
Tweet

More Decks by Suat Köse

Other Decks in Programming

Transcript

  1. Add OData Support to Your Asp.Net Core Web Api Email

    : [email protected] Blog : medium.com/@suadev Code : github.com/suadev Tweet : kose__suat Suat Köse Nov 2th, 2019 İstanbul - Dotnetkonf
  2. AGENDA • What is OData and Why Do We Need

    It? • OData Query String Parameters & Filter Expressions • Consuming OData Services • OData & Asp.Net Core Web Api • Demo (public repo at github.com/suadev)
  3. OData is a protocol that allows creation and consumption Rest

    Apis in a standard way. By using OData, you can convert your api to a queryable api. What is OData ? (Open Data Protocol) 2007 Initiated by Microsoft. Approved as a standard by OASIS. ASP.NET Core OData released in June. 2014 2018
  4. - Allows to create easy to consume RESTful apis. -

    Provides access with minimal effort to database for CRUD operations. - Filtering, Sorting, Paging and Projection are effortless! - Allows you to focus more on business logic when developing APIs. - Useful Metadata document. (http://localhost/odata/$metadata) Why OData?
  5. Why OData? Projection Name City Birth Date Status Record Date

    William Poirot Tokyo 1972-06-19 Active 2019-06-22 Daniel Nolan Stockholm 2013-12-24 Passive 2018-11-22 http://localhost/api/users http://localhost/api/? Name William Poirot Daniel Nolan Name Status William Poirot Active Daniel Nolan Passive http://localhost/api/?
  6. Why OData? Filtering Search box… (name, category and barcode) Price

    ❏ Under $25 ❏ $25 to $50 ❏ $50 to $100 ❏ $100 to $200 ❏ $200 & Above $ Min $ Max Barcode Product Name ^ Category ^ Price Expire Date ❏ Expired Product List - A complex search api that should respond to all clients ? ... more refinement ...
  7. Most Used OData Query String Parameters $select localhost/odata/products?$select=id,productname $count localhost/odata/products?$count=true

    $expand localhost/odata/products?$expand=category $orderby localhost/odata/products?$orderby=price $skip localhost/odata/products?$skip=5 $top localhost/odata/products?$top=5 $filter localhost/odata/products?$filter=price gt 10000
  8. OData Filter Expressions equal $filter=productName eq ‘TV’ less than $filter=price

    lt 2000 greater than $filter= price gt 1000 and / or $filter=price gt 1000 and price lt 2000 startswith $filter=startswith(productName, ’PC’) length $filter=length(productName) gt 20 indexof $filter=indexof(description, ’LED’) ge 0
  9. OData Filter Expressions toupper $filter=toupper(productName) eq '32 INCH TV' trim

    $filter=trim(category) eq ‘Notebook' day $filter=day(creationDate) eq 12 month $filter=month(creationDate) eq 12 year $filter=year(creationDate) eq 2010 round $filter=round(weight) eq 1 floor $filter=floor(weight) eq 0 ceiling $filter=ceiling(weight) eq 1
  10. OData & Asp.Net Core Web Api - Version 3.0 Doesn’t

    Support 3.0 Yet. .Net Core 3.0 changed routing and OData tied to how Asp.Net does routing. They are working on it. ⚠
  11. OData & Asp.Net Core Web Api - Use Cases Your

    api can consist of ; - Only OData endpoints for all CRUD operations. - OData for Query part and Non-OData for Command part. So, you may want to use OData only for supercharging your api’s Query part. You are also free to add OData support for only some specific services.
  12. OData & Asp.Net Core Web Api - Basic Setup 2

    3 4 1 Only OData endpoints are exist. OData route is disabled. Returns IQueryable