Skip to content
James Crosswell edited this page Apr 3, 2016 · 8 revisions

Overview

Command Routing can be used to route HTTP requests to pipelines of request handlers. By contrast, the standard MVC 6 routing mechanism routes requests to action methods that you define on MVC Controller classes.

Here's an example from the Sample project:

var routeBuilder = new CommandRouteBuilder(app.ApplicationServices);

routeBuilder
    .Get("hello/{name:alpha}")
    .As<SayHelloRequest>()
    .RoutesTo<IgnoreBob, SayHello>();

app.UseRouter(routeBuilder.Build());

So what's going on in that example?

The Route

Firstly, we create an instance of CommandRouteBuilder:

var routeBuilder = new CommandRouteBuilder(app.ApplicationServices);

Then we create a route that will listen for HTTP GET requests on any route matching the template "hello/{name:alpha}".

    .Get("hello/{name:alpha}")

This example uses GET, but all the other HTTP verbs are also supported of course.

The Request Message

The next line tells Command Routing to interpret all incoming requests as being of type SayHelloRequest:

    .As<SayHelloRequest>()

SayHelloRequest can be any POCO with a parameterless constructor. Command Routing will create an instance of SayHelloRequest automatically from the HTTP Request... so it's basically a strongly typed interpretation of the HTTP Request.

The Request Pipeline

Finally, the last line tells Command Routing to send all requests that it receives on the route to a Request Pipeline.

    .RoutesTo<IgnoreBob, SayHello>();

A request pipeline is an ordered series of Request Handlers capable of handling the same Request type. In this case our pipeline consists of two handlers: IgnoreBob and SayHello, both of which can handle SayHelloRequest messages.

When handling the route, Command Routing passes the request message to each of the handlers in the pipeline until one of them indicates that it has handled the request. At that point, Command Routing exits the pipeline and prepares an appropriate HTTP Response.

Clone this wiki locally