The ASP.NET Core MVC Pipeline

Hallo Iedereen 🙂
I decided to start a series of posts about custom components for the ASP.NET Core MVC Pipeline.

Most of you should know by now that ASP.NET Core was mostly redesigned to be more modular, extensible and to deal with the ‘platform agnostic’ nature of the new framework. This redesign also allows us to customize the way every request is processed, adding custom components in each step of the pipeline.

The ASP.NET Core MVC Pipeline

The ASP.NET Core MVC Pipeline
The ASP.NET Core MVC Pipeline

The idea is to have one post for each of the small blocks on the pipeline, precisely because it is possible to hook up your custom code in any of those points.

I’ll start by providing a brief overview of which is the role of each block on the pipeline:

Middleware Pipeline

Middleware Pipeline
Middleware Pipeline

The middleware pipeline is the entry point for each request in an HTTP Request, it is basically a stack of components that can execute some logic and decide whether to pass the request to the next middleware, or to send the response back to the client.

Routing Middleware

The routing middleware is just one of many that can be used by the ASP.NET Core but is of major importance to the Core MVC Pipeline. The reason for this it that this is the Middleware that ‘captures’ an incoming request and really starts the MVC Pipeline. If an incoming request url does not match any registered Route the request is passed onward to the next Middleware, ignoring the MVC Pipeline, but when the request url matches a known route, that is when the MVC Pipeline really begins.

Controller Initialization

Controller Initialization
Controller Initialization

Once a route is found by the Routing middleware, the next step is to find a controller that matches the route and instantiate it. The process of finding the best suitable Controller and Action for a Route can also be customized.

Action Execution

Action Execution
Action Execution

Now we are in the core of what really is the MVC pattern. The Action Execution is the process that will really process the incoming request and, after that, generate a Result. This is also the process where we can hook up most of our components.

Model Binding

One of the greatest features on the MVC Framework is the ability to bind request data to POCO Objects. I’ll try to explain a bit about how the standard Models Binders work, and how you can create your own Model Binder.

Filters

Filters are components that can be injected at many stages of the Action Execution and that will allow us to define behaviors that we want in our Actions, but that are not necessarily part of the LOB Logic.

Action Result

Finally, the action result is the last step of the Action Execution, and it involves basically to create a ‘Result’ type that will be processed by the Result Execution.

Result Execution

Result Execution
Result Execution

And the last step in the Core MVC Pipeline has arrived! Here is where our ‘Result’ will be analysed and processed accordingly. Here we can also inject some filters before and after our processing, allowing us to change the way the result will be processed.

Where do we start?

From the beginning! Of course 🙂 We will start creating a Custom Middleware that will be executed before the Routing Middleware.

The first post will be up very soon!

Tot ziens! (And I just spent all my Dutch in this post)

Source:

Also published on Medium.

3 thoughts on “The ASP.NET Core MVC Pipeline”

  1. There shouldn’t be direct arrow between “Result execution” block and “Http response”. Instead there should be arrow from “Result execution” block to “Middleware pipeline” block and then from “Middleware pipeline” block to “HTTP Response”. Middleware are also processed at the end in reversed order.

    1. Hi Darek, thanks for the comment!
      You are right on the fact that the Middledware Pipeline is also used on the response flow but, whenever the MVC middleware catches the Request, it will handle it inside its own flow, and not give it back to the middleware pipeline.
      I will double-check documentation and the framework code to make sure I am giving you the right information, and if I find it differently I’ll update the post.

  2. Running the following code will give: “No content middleware response”, so it seems that middleware
    can be run also after MVC pipeline, at least in Core 2.1.

    public class Startup {

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    app.UseMiddleware();
    app.UseMvcWithDefaultRoute();
    }
    }

    public class HomeController : Controller {
    public IActionResult Index() => NotFound();
    }

    public class ErrorMiddleware {
    private RequestDelegate nextDelegate;
    public ErrorMiddleware(RequestDelegate next) { nextDelegate = next; }

    public async Task Invoke(HttpContext httpContext) {
    await nextDelegate.Invoke(httpContext);
    if (httpContext.Response.StatusCode == 404)
    await httpContext.Response.WriteAsync(“No content middleware response”, Encoding.UTF8);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.