Вы находитесь на странице: 1из 28

Web API Basics

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Objectives
See how REST and web services can be used to access data Build your first Web API service Use Web API naming conventions for routing Modify your service for basic CRUD operations Filter your data results with URL parameters

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Agenda
What Is ASP.NET Web API? Create Your First Web API Service Web API Routing Create a Web API Service for CRUD Operations Filtering Data with Parameters

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

What Is ASP.NET Web API?


Next iteration of WCF REST Incorporated into ASP.NET MVC 4 Framework for developing REST oriented services

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

REST
Uses HTTP protocols URLs and methods Accessible from a wide variety of clients HTTP methods:

GET POST PUT DELETE


Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company

GET
Primarily just a URL making a simple request for a resource, e.g., a web page Response is sent back Additional information is put in a query string Generally used to Select data Selecting all: http://www.root.com/products/ Selecting one: http://www.root.com/products/17
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company

POST
Sends collection of name-value pairs along with the request Commonly used with forms on the web Used to add new resources (Insert) Should return an appropriate HTTP success code

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

PUT
Used to store a resource at the supplied URL Generally used for Editing existing resources

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

DELETE
Used for deleting resources Should return an appropriate HTTP code

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Agenda
What Is ASP.NET Web API? Create Your First Web API Service Web API Routing Create a Web API Service for CRUD Operations Filtering Data with Parameters

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Create Your First Web API Service


Create a repository Create an API controller to use the repository Try it out in a browser

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Agenda
What Is ASP.NET Web API? Create Your First Web API Service Web API Routing Create a Web API Service for CRUD Operations Filtering Data with Parameters

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Web API Routing


Interpretation of URLs by the server to decide what code should handle the request Operates similarly to ASP.NET MVC routing Actions determined by HTTP method used

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Default Mapping
Configured in Global.asax Uses api as URL segment Adds word Controller to the controller part of URL Looks for action that begins with the HTTP method Additional URL parameters are mapped as action parameters
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company

Alternative Routing Conventions


Rarely necessary for most Web API services Several options to change URL routing

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

HTTP Method Attributes


Very similar to ASP.NET MVC Use attributes to bind HTTP methods to specific actions [HttpGet] [HttpPost] [HttpPut] [HttpDelete]
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company

AcceptVerbs Attribute
Also used on actions Specify HTTP methods as strings Especially useful for atypical HTTP methods

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Action Names in the URL


Makes URLs route identically to ASP.NET MVC Must change routeTemplate in Global.asax Must also add HTTP method attributes to actions

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Removing Actions from Routing


Used when a resources URL would trigger an action, but this is not desired Use the NonAction attribute on the method

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Agenda
What Is ASP.NET Web API? Create Your First Web API Service Web API Routing Create a Web API Service for CRUD Operations Filtering Data with Parameters

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Create a Web API Service for CRUD Operations


Create POST Read GET Update PUT Delete DELETE Basic functionality that most services will have Try it out!
Learn More @ http://www.learnnowonline.com
Copyright by Application Developers Training Company

Agenda
What Is ASP.NET Web API? Create Your First Web API Service Web API Routing Create a Web API Service for CRUD Operations Filtering Data with Parameters

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Filtering Data with Parameters


Uses the OData protocol Special query strings can be used to:
Filter Page

Sort
And more

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Filter
Condition that evaluates to true or false Uses OData-specific keywords Must be sendable in a URL

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Order By
Specifies the sort order of returned data Can sort by multiple properties Can specify ASC and DESC sorting

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Paging
Done by using $skip and $top $skip passes over a number of records $skip should be set to: page number * page size $top selects a certain number to return $top should be set to page size

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Theres More
There are other OData query strings Other query keywords for $filter www.odata.org

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Learn More!
This is an excerpt from a larger course. Visit www.learnnowonline.com for the full details!

Learn More @ http://www.learnnowonline.com


Copyright by Application Developers Training Company

Вам также может понравиться