You are on page 1of 1

ASP.

NET WEB API 2: HTTP MESSAGE LIFECYLE


You can host Web API inside IIS or
ASP.NET Web API is a framework that makes it easy to build HTTP
inside your own process (self-hosting). IIS Hosting
OWIN
Self-Hosting
services that reach a broad range of clients, including browsers and A Create Controller
Create an API controller based on the request.
mobile devices. It is an ideal platform for building RESTful
applications on the .NET Framework. 1. Select controller type

The HTTP request message is first


This poster shows how an HTTP request flows through the Web API HttpControllerDispatcher
converted to an HttpRequestMessage HTTP Request HTTP Response pipeline, and how the HTTP response flows back. The diagram also
object, which provides strongly typed
access to the HTTP message. shows extensibility points, where you can add custom code or even SelectController HttpControllerDescriptor

replace the default behavior entirely. You can find documentation


and tutorials for ASP.NET Web API at http://www.asp.net/web-api. IHttpControllerSelector
HttpServer
GetControllerTypes ICollection<Type>

HttpRequestMessage HttpResponseMessage IHttpControllerTypeResolver

GetAssemblies ICollection<Assembly>

HTTP Message Handlers


HTTP message handlers are the first stage in IAssembliesResolver
the processing pipeline. They process HTTP
request messages on the way in, and HTTP
response messages on the way out. A message handler can create the response
DelegatingHandler directly, skipping the rest of the pipeline.
To create a custom message handler, derive
from the DelegatingHandler class. You can 2. Activate controller
add multiple message handlers.
Per-route
Message handlers can be global or assigned Message Handlers HttpControllerDispatcher
to a specific route. A per-route message
handler is invoked only when the request
matches that route. Per-route message HttpRoutingDispatcher Route.Handler Create IHttpController
handlers are configured in the routing table.

No IHttpControllerActivator
Route.Handler DelegatingHandler
is null?
Yes

A message handler can create This message handler can invoke B Select Controller Action
the response directly, skipping HttpControllerDispatcher HttpMessageHandler HttpControllerDispatcher and return to the
the rest of the pipeline. main path, or provide a custom end point. Select an action based on the request.

A Create API ApiController


controller
SelectAction HttpActionDescriptor

Controller Select controller


B IHttpActionSelector
The controller is where you define the main logic action
for handling an HTTP request. Your controller
derives from the ApiController class.
Authentication Filters
E Invoke Controller Action
AuthenticateAsync ChallengeAsync Invoke controller action, using HttpActionContext
for bindings and model state.

Error response
ApiController
If the request is not authorized, an
authorization filter can create an error Authorization Exception
response and skip the rest of the pipeline. Filters Filters InvokeActionAsync Task<HttpResponseMessage>

C D
IHttpActionInvoker
Model Binding Result Conversion

Action filters are invoked


twice, before and after the Action Filters
controller action.
Key
Built-in Class
OnActionExecuting OnActionExecuted
Exception!
Unhandled exceptions are Extensibility Point
routed to exception filters.

Note
E Invoke Action

Request

Controller Action Response

C D
HttpRequestMessage
Model Binding Result Conversion
Request message HttpResponseMessage
Model binding uses the request to The return value from the
create values for the parameters of the action is converted to an
action. These values are passed to the URI HttpResponseMessage.
action when the action is invoked.
Headers
Entity-body

Media Type Formatter


FormatterParameterBinding ModelBinderParameterBinding HttpParameterBinding

IContentNegotiator

A media-type formatter The default model binders A custom parameter


reads the message body read from the URI path binding can read any part
(if any). and query string. of the HTTP request.

If return type is For all other return


IHttpActionResult, types, a media-type
If return type is If return type is void, call ExecuteAync formatter serializes the
Media Type Formatter IModelBinder IValueProvider value and writes it to the
HttpResponseMessage, create response with to create an
pass through. status 204 (No Content). HttpResponseMessage message body.

Complex Type Simple Type Any Type HttpResponseMessage void IHttpActionResult Other types

Action parameters Action return value

Email: MSPoster@microsoft.com
2014 Microsoft Corporation. All rights reserved.

You might also like