Skip to content

Routing

Martin Halliday edited this page Jul 8, 2016 · 3 revisions

The OWIN Framework comprises mostly interfaces but it also contains a pipeline builder and one of the features of the pipeline builder is the ability to split the OWIN pipeline into multiple segments. This is referred to as routing. The pipeline builder places middleware into routing segments in such a way that middleware only executes on routes where it is required, and middleware will execute before any other middleware that depends on it.

The application developer can define routes by constructing instances of IRouter and adding them to the OWIN pipeline. IRouter is just a regular middleware class and implements IMiddleware<IRoute> but when it processes the request instead of simply passing control to the next middleware in the chain, it first selects a segment and pushes the request through that chain of middleware before chaining the next middleware in the segment it is on.

The OWIN Framework defines an extension method for IMiddleware<IRoute> called AddRoute() that allows the application developer to add routes (segments) to the router. As well as the AddRoute() extension method any other extension methods can be applied to routers because they are just regular middleware.

When application developers call the AddRoute() extension method on a router they must specify the name of the route and a lambda expression that determines if a request should be routed down that segment. This is a sample of some code that configures some routes. For more examples see the example usage project in the source code.

    builder.Register(ninject.Get<IRouter>())
      .AddRoute("ui", context => context.Request.Path.Value.EndsWith(".aspx"))
      .AddRoute("staticFiles", context =>
        {
          var path = context.Request.Path.Value;
          var fileExtensionIndex = path.LastIndexOf('.');
          if (fileExtensionIndex < 0) return false;

          var fileExtension = path.Substring(fileExtensionIndex).ToLower();
          if (fileExtension == ".html") return true;
          if (fileExtension == ".css") return true;
          if (fileExtension == ".js") return true;
          if (fileExtension == ".jpg") return true;
          if (fileExtension == ".png") return true;
          return false;
        })
      .AddRoute("api", context => context.Request.Path.Value.StartsWith("/api/"))
      .AddRoute("invalid", context => true)
      .As("Request type router");

The OWIN Framework defines the following extension methods that extend IMiddleware<T> allowing the application developer to configure routing.

Extension method Description
As() Assigns a name to the middleware
RunAfter() Specifies that this middleware depends on another middleware. There are overloads to specify the dependency by type or by name or both
RunFirst() Tells the pipeline builder to run this middleware before the first routing split so that this middleware will always run on every route.
RunLast() Tells the pipeline builder to run this middleware on the leaves of every routing segments. See notes below
RunOnRoute() Tells the pipeline builder that this middleware must be on a specific route.

Notes

  1. If you want to specify the name of the middleware to RunAfter() then you must name the middleware it depends on using the As() extension method.
  2. Two middleware instances can not have the same name. Names are not case sensitive.
  3. When you use the RunFirst() extension method you can not use RunLast() or RunOnRoute() because this would be contradictory. You can however combine the As() and RunAfter() extensions with RunFirst().
  4. You can call the RunOnRoute() extension multiple times for the same middleware. This will place the middleware on both routes and effectively join two or more segments of the pipeline together. You can also create more than one instance of the middleware and assign them to different routes. This is especially useful when you want different configurations for the different routes.
  5. You can call the RunLast() method in combination with one or more calls to RunOnRoute(). This will make the middleware run after all other middleware on the specified routes.

Clone this wiki locally