Your SlideShare is downloading. ×
0
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Web api routing
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Web api routing

871

Published on

Asp.net Web api routing

Asp.net Web api routing

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
871
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 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
  • 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 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
  • 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

×