KEMBAR78
Web api routing | PPTX
Web API Routing


  Eyal Vardi
  CEO E4D Solutions LTD
  Microsoft MVP Visual C#
  blog: www.eVardi.com
Expert Days 2012
                                                                                 




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Routing Has Three Main Phases

    1.      Matching the URI to a route template.

    2.      Selecting a controller.

    3.      Selecting an action.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Routing Tables
            routes.MapHttpRoute(
                   name:                       "DefaultApi",
                   routeTemplate:              "api/{controller}/{id}",
                   defaults:                   new { id = RouteParameter.Optional }
                   constraints:                new { id = @"d+" }
            );


                         Action                       HTTP method                        Relative URI
         Get a list of all products                   GET                        /api/products
         Get a product by ID                          GET                        /api/products/id

         Get a product by category                    GET                        /api/products?category=1




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Action Mapping
            public class ProductsController : ApiController
            {
                public void GetAllProducts() { }
                public IEnumerable<Product> GetProductById(int id) { }
                public HttpResponseMessage DeleteProduct(int id) { }
            }




               Method                   URI Path                        Action       Parameter
          GET                    api/products                 GetAllProducts     (none)
          GET                    api/products/4               GetProductById     4
          DELETE                 api/products/4               DeleteProduct      4
          POST                   api/products                 (no match)



© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Routing by Action Name
           routes.MapHttpRoute(
               name: "ActionApi",
               routeTemplate: "api/{controller}/{action}/{id}",
               defaults: new { id = RouteParameter.Optional }
               );


           public class ProductsController : ApiController
           {
               [AcceptVerbs("GET", "HEAD")]
               public Product FindProduct(id) { }

                 // WebDAV method
                 [AcceptVerbs("MKCOL")]
                 public void MakeCollection() { }
           }




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Web API Routing


© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Paging and Querying (OData)
           ASP.NET Web API provides built-in support
            for OData query parameters.
                  $filter, $orderby, $skip & $top


           Examples
                  /api/products?$filter=substringof(Name, 'Ed') eq true
                  api/products?$skip=2
                  api/products?$top=3&$orderby=Name

                     [Queryable(ResultLimit=20)]
                     public IQueryable<Product> GetAllProducts()
                     {
                         return repository.GetAll().AsQueryable();
                     }

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Query string options
           $Expand
                  /Customers(ALFKI)?$expand=Orders.Employees

           $Orderby
                  /Customers?$orderby=City desc, CompanyName

           $Skip
                  /Customers?$skip=10

           $Top
                  /Orders?$orderby=TotalDue&$top=5

           $Filter
                  /Customers?$filter=City eq ‘London’

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Expression Syntax
      Operator           Description                                             Example
                                              Logical Operators
         eq        Equal                 /Customers?filter = City eq 'London'
         ne        Not equal             /Customers?filter = City ne 'London'
         gt        Greater than          /Product?$filter = UnitPrice gt 20
        gteq       Greater than or equal /Orders?$filter      = Freight gteq 800
         lt        Less than             /Orders?$filter      = Freight lt 1
        lteq       Less than or equal    /Product?$filter = UnitPrice lteq 20
        and        Logical and           /Product?filter=UnitPrice lteq 20 and UnitPrice gt 10
         or        Logical or            /Product?filter=UnitPrice lteq 20 or UnitPrice gt 10
        not        Logical negation      /Orders?$ ?$filter=not endswith(ShipPostalCode,'100')
                                             Arithmetic Operators
         add       Addition              /Product?filter = UnitPrice add 5 gt 10
         sub       Subtraction           /Product?filter = UnitPrice sub 5 gt 10
         mul       Multiplication        /Orders?$filter = Freight mul 800 gt 2000
         div       Division              /Orders?$filter = Freight div 10 eq 4
         mod       Modulo                /Orders?$filter = Freight mod 10 eq 0
                                             Grouping Operators
         ( )        Precedence grouping /Product?filter = (UnitPrice sub 5) gt 10




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Expression Syntax Cont.
    String Functions                                                             Date Functions
    bool contains(string p0, string p1)                                          int day(DateTime p0)
    bool endswith(string p0, string p1)                                          int hour(DateTime p0)
    bool startswith(string p0, string p1)                                        int minute(DateTime p0)
    int length(string p0)                                                        int month(DateTime p0)
    int indexof(string arg)                                                      int second(DateTime p0)
    string insert(string p0, int pos, string p1)                                 int year(DateTime p0)
    string remove(string p0, int pos)
    string remove(string p0, int pos, int length)
    string replace(string p0, string find, string replace)                       Math Functions
    string substring(string p0, int pos)                                         double round(double p0)
    string substring(string p0, int pos, int length)                             decimal round(decimal p0)
    string tolower(string p0)                                                    double floor(double p0)
    string toupper(string p0)                                                    decimal floor(decimal p0)
    string trim(string p0)                                                       double ceiling(double p0)
    string concat(string p0, string p1)                                          decimal ceiling(decimal p)


     Type Functions
     bool IsOf(type p0)
     bool IsOf(expression p0, type p1)
     <p0> Cast(type p0)
     <p1> Cast(expression p0, type p1)

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Data Services
          URL Syntax

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

Web api routing

  • 1.
    Web API Routing Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  • 2.
    Expert Days 2012  © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 3.
    Routing Has ThreeMain Phases 1. Matching the URI to a route template. 2. Selecting a controller. 3. Selecting an action. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 4.
    Routing Tables routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } constraints: new { id = @"d+" } ); Action HTTP method Relative URI Get a list of all products GET /api/products Get a product by ID GET /api/products/id Get a product by category GET /api/products?category=1 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 5.
    Action Mapping public class ProductsController : ApiController { public void GetAllProducts() { } public IEnumerable<Product> GetProductById(int id) { } public HttpResponseMessage DeleteProduct(int id) { } } Method URI Path Action Parameter GET api/products GetAllProducts (none) GET api/products/4 GetProductById 4 DELETE api/products/4 DeleteProduct 4 POST api/products (no match) © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 6.
    Routing by ActionName routes.MapHttpRoute( name: "ActionApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); public class ProductsController : ApiController { [AcceptVerbs("GET", "HEAD")] public Product FindProduct(id) { } // WebDAV method [AcceptVerbs("MKCOL")] public void MakeCollection() { } } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 7.
    Web API Routing ©2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 8.
    Paging and Querying(OData)  ASP.NET Web API provides built-in support for OData query parameters.  $filter, $orderby, $skip & $top  Examples  /api/products?$filter=substringof(Name, 'Ed') eq true  api/products?$skip=2  api/products?$top=3&$orderby=Name [Queryable(ResultLimit=20)] public IQueryable<Product> GetAllProducts() { return repository.GetAll().AsQueryable(); } © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 9.
    Query string options  $Expand  /Customers(ALFKI)?$expand=Orders.Employees  $Orderby  /Customers?$orderby=City desc, CompanyName  $Skip  /Customers?$skip=10  $Top  /Orders?$orderby=TotalDue&$top=5  $Filter  /Customers?$filter=City eq ‘London’ © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 10.
    Expression Syntax Operator Description Example Logical Operators eq Equal /Customers?filter = City eq 'London' ne Not equal /Customers?filter = City ne 'London' gt Greater than /Product?$filter = UnitPrice gt 20 gteq Greater than or equal /Orders?$filter = Freight gteq 800 lt Less than /Orders?$filter = Freight lt 1 lteq Less than or equal /Product?$filter = UnitPrice lteq 20 and Logical and /Product?filter=UnitPrice lteq 20 and UnitPrice gt 10 or Logical or /Product?filter=UnitPrice lteq 20 or UnitPrice gt 10 not Logical negation /Orders?$ ?$filter=not endswith(ShipPostalCode,'100') Arithmetic Operators add Addition /Product?filter = UnitPrice add 5 gt 10 sub Subtraction /Product?filter = UnitPrice sub 5 gt 10 mul Multiplication /Orders?$filter = Freight mul 800 gt 2000 div Division /Orders?$filter = Freight div 10 eq 4 mod Modulo /Orders?$filter = Freight mod 10 eq 0 Grouping Operators ( ) Precedence grouping /Product?filter = (UnitPrice sub 5) gt 10 © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 11.
    Expression Syntax Cont. String Functions Date Functions bool contains(string p0, string p1) int day(DateTime p0) bool endswith(string p0, string p1) int hour(DateTime p0) bool startswith(string p0, string p1) int minute(DateTime p0) int length(string p0) int month(DateTime p0) int indexof(string arg) int second(DateTime p0) string insert(string p0, int pos, string p1) int year(DateTime p0) string remove(string p0, int pos) string remove(string p0, int pos, int length) string replace(string p0, string find, string replace) Math Functions string substring(string p0, int pos) double round(double p0) string substring(string p0, int pos, int length) decimal round(decimal p0) string tolower(string p0) double floor(double p0) string toupper(string p0) decimal floor(decimal p0) string trim(string p0) double ceiling(double p0) string concat(string p0, string p1) decimal ceiling(decimal p) Type Functions bool IsOf(type p0) bool IsOf(expression p0, type p1) <p0> Cast(type p0) <p1> Cast(expression p0, type p1) © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 12.
    Data Services URL Syntax © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il