Skip to content

IRoutingProcessor

Martin Halliday edited this page Jul 14, 2016 · 2 revisions

Middleware can implement this interface if it wants to be called during the routing phase of request processing.

The pipeline builder class in the OWIN Framework builds an OWIN pipeline that takes different paths for different types of requests. Sometimes middleware near the front of the pipeline needs to get information from middleware further down the pipeline and therefore needs to know which path the request is going to take through the pipeline before it can execute. For this reason the routing phase takes place first, then after the route is established and the middleware that will process the request is known there is a second processing phase where a response it generated and sent back to the browser.

Even when there are no routes configured the routing phase is still required. Imagine a situation in which there is session middleware that runs near the front of the pipeline that needs to know if it should retrieve session data from the session server or not. Imagine also that we have a static file middleware that does not need session and a template page rendering middleware that needs session for some pages and not others. Until we know if this request is for a static file or a page template the session middleware does not know whether to establish session or not - but only the static file middleware and the template page rendering middleware know how to determine if the request is for them or not.

Assuming that the pipeline is built in this order: session -> static files -> template rendering

The processing of a request will proceed as follows:

  1. The routing phase starts by calling the RouteRequest() method of the session middleware. It places an implementation of IUpstreamSession into the OWIN context.

  2. The RouteRequest() method of the static files middleware is called. It determines if this request is for a static file and if so returns without chaining to the next middleware, in this case the template rendering middleware does not get executed.

  3. If the request is not for a static file then the RouteRequest() method of the page rendering middleware is called. It determines if this is a request for a page template and if this page template needs session. If session is required it gets the IUpstreamSession implementation from the OWIN context and calls its EstablishSession() method.

  4. The request processing phase starts by calling the Invoke() method of the session middleware. The session middleware determines if the EstablishSession() method of IUpstreamSession was called or not, and if it was then it retrieves session data from the session server.

  5. The Invoke() method of the static file middleware is called. If this request is for a static file then the file contents are returned to the browser and the static file middleware does not chain to the next middleware in the pipeline.

  6. If this was not a request for a static file then the Invoke() method of the page rendering middleware is called. The page template rendering middleware processes the request using session as necessary.

  7. As each method in the chain returns and the stack unwinds the session middleware can write any changes in session back to the session server.

Clone this wiki locally