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
System.Web.Http.WebHost.dll
Default Hosting Process
App_Startup -> WebApiConfig.cs
Web-hosted Web API only supports a single server and single configuration file.
Global.asax - GlobalConfiguration.Configuration to config
SELF HOSTED WEB API
WEB API 2.0
System.Web.Http.SelfHost.dll
Hosting inside of a console application/GUI application/Windows Service.
NuGet - Microsoft.AspNet.WebApi.SelfHost -> System.Net.Http, System.Web.Http
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