Date post: | 12-Dec-2014 |
Category: |
Technology |
Upload: | dotnet-miami |
View: | 727 times |
Download: | 3 times |
Building HTTP based Web Services with ASP.NET Web API
Cecil L. Phillip
@cecilphillip
Overview
Short Review of HTTP
ASP.NET Web Stack
Web API Pipeline
Web API Extensibility Points
HTTP Tools
DEMOS!!
Why HTTP Services ?
Light Weight
Support for Multiple Formats
HTTP Caching
Accessible from a variety devices
HTTP Messages
Request and Response Messages consist of An Initial Start Line Zero or More Message Headers An Optional Message Body
RFC 2616 - HTTP 1.1 Spec http://www.w3.org/Protocols/rfc2616/rfc2616.html
HTTP Request Methods Identifies the Action against the Resource
The HTTP Spec defines eight Method Types
A few of these Methods include: POST – Create a new Resource GET – Retrieve the Resource PUT – Create or Update a Resource DELETE – Delete the Resource
Most Web Browsers only support GET and POST
HTTP Headers Provide valuable meta data for the message
Some Standard Headers include: Accept – Accepted formats by the Client Content-Type – Format associated with the request
body Host – Server domain name User-Agent – Client software identifier
Additional fields may be added by the Server or Application
HTTP Headers cont.. Common Uses Include
Setting Cookies Defining Caching Passing Authentication credentials
Non standard header fields are prefixed with “X-”
IANA Header Field Name Registry http://goo.gl/lcUlU
HTTP Status Codes Identify the type of request success and
failures
Some codes you might see: 2xx – Successful Response
201 - Created 3xx – Redirect Response
301 – Moved Permanently 4xx – Client Error Response
401 – Unauthorized 5xx – Service Error Response
503 – Service Unavailable
Web API Lineage
ASP.NET STACK
ASP.NET Core
Sites
MVC Web Forms
Web Pages
Singe Page Apps
Services
Web API SignalR
Web API and MVC Build Web Applications with MVC and Web Services
with Web API
ASP.NET Web API is not dependent on ASP.NET MVC
Shared Concepts between the Frameworks Controllers Filters Model Binding Model Validation Routing Dependency Resolver
Web API Framework Features
Strongly Typed API for working with HTTP Messages
Content Negotiation Pluggable Messaging Pipeline Code Based Configuration IoC Support OData Query Composition Flexible Hosting Options
Where Do I Get It?
NuGet
Web Platform Installer
Microsoft Download Center
Web API Pipeline
Http Client
Message
Handler
Message
Handler
Http Client Handle
r
Http Server
Message
Handler
Message
Handler
Http Dispatcher
API Client Side
API Server Side
Web API Dispatcher
Invoke Action
Model Binding
Action Selection
Controller Selection
Request
Exception Filters
Formatting
Response
Authorization Filters
Action Filters
Media Types Used to Identify Resources Format Types
Used in Various Networking Protocols e.g. HTTP, RTP, SMTP
Vendor Specific Formats begin with vnd.
Web API supports XML, JSON, and Form-Urlencoded data out of the box
IANA Media Type Listinghttp://www.iana.org/assignments/media-types
Content Negotiation
Service Clients tell the Server about Desired Formats
Servers returns Resources in Supported Format
ASP.NET Web API supports XML and JSON out of the box
Additional Formats can easily plugged in
Filters Borrowed concept from ASP.NET MVC
Replaced Operation Handlers from the Web API Previews
Can be applied on various levels Per Controller, Per Action, or Global
Filter Types Action Filters – executes before and/or after an
operation Authorization Filters – executes before model
binding Exception Filters – catch unhandled operation
exceptions
Message Handlers Provides a Pipeline for processing HTTP
Messages
Allows for Inspection and Manipulation of HTTP Messages in transit
Common Uses: Logging / Tracing Adding and Removing HTTP Headers Message Filtering
Securing Your Service
Enable HTTPS / SSL on the Servers
Generate API Keys
Implement Authentication Tokens
Use Key Encryption
Hosting Options
ASP.NET Web Hosting Uses the HttpControllerHandler async handler
Self Hosting Translates messages using a WCF channel stack
In-Memory Hosting Good for testing
HttpClient Configurable class for sending
HttpRequestMessages
Originally in the Rest Starter Kit [2009]
Only Asynchronous Public Methods
Included in .NET 4.5
Available in the System.Net.Http NuGet Package
HTTP Development Tools
Curl
Fiddler Web Debugger
Wireshark
Desktop Web Browser
Alternatives Nancy
http://nancyfx.org
ServiceStackhttp://servicestack.net
OpenRastahttp://openrasta.org
WCF Web HTTPhttp://goo.gl/IRgM7
Resources ASP.NET Web API Homepage http://
www.asp.net/web-api
Getting Started With ASP.NET Web APIhttp://bit.ly/IoFApW
ASP.NET CodePlex Repositoryhttp://aspnetwebstack.codeplex.com
Twitter #aspnetwebapi
THANK YOU