WEB API 2.
BY
AMARESWARA RAO
INTRODUCTION
WEB API 2.0
Why Web API ?
For building RESTful applications on the .NET Framework
What are the features of WEB API 2.0 ?
Attribute Routing
Self Host
IHTTPActionResult
Filter Overrides
Testability
CONTENTS
WEB API 2.0
Self-Hosted Web API
Web-Hosted Web API
Global vs Attribute Routing
Verb Names/Action Methods
Binding Parameters
Filtering Requests
HTTP Status Codes with IHTTPActionResult
WEB HOSTED WEB API
WEB API 2.0
[Link]
Default Hosting Process
App_Startup -> [Link]
Web-hosted Web API only supports a single server and single configuration file.
[Link] - [Link] to config
SELF HOSTED WEB API
WEB API 2.0
[Link]
Hosting inside of a console application/GUI application/Windows Service.
NuGet - [Link] -> [Link], [Link]
console app - place the code in main function
VERB NAMES/ACTION METHODS
WEB API 2.0
Verb Names : Get , Post , Put , Delete , Head , Patch , and
Options
Default Verb : POST
[HttpXyz]
Allowed verbs : [AcceptVerb("Get , Post , Put , Delete")]
DELETE
PUT
POST
GET
BINDING PARAMETERS
WEB API 2.0
action method signature and include parameters, complex types/
simple types
simple types -> int/string -> not from the body(model binders are
responsible)
complex types -> class types -> from the body (formatters are
responsible)
Parameter Binding : [ModelBinding],[FromUri],[FromBody]
FILTERING REQUESTS
WEB API 2.0
Filters are now part of the asynchronous pipeline
[AuthorizeAttribute] X [AllowAnonymousAttribute]
AuthorizationFilterAttribute : filter run before any parameter
binding has happened And Authorization filters run before action
filters.
ActionFilterAttribute : filters run after parameter binding has
happened
ExceptionFilterAttribute : new response object
IHTTPACTIONRESULT
WEB API 2.0
IHTTPActionResult