ASP.NET Core Middleware Part – II

Posted: April 12, 2020 in .NET, ASP.NET Core
Tags: , ,

In the last post we have discussed the basics of ASP.NET Core Middleware and how it fits into the request processing pipeline. As a next step we will see how we can build the middleware pipeline using request delegates that contain our custom code.

These request delegates are configured in the Configure method of the Startup class. The three extension methods of IApplicationBuilder that are used to build and configure the middleware pipeline are as follows:

  • Run – This method add a request delegate in the pipeline which terminates the middleware pipeline.
  • Use – This method adds a request delegate in the middleware pipeline which processes the request and passes it onto the next delegate in chain. Similarly processes the response and passes onto the previous delegate in chain (if any).
  • Map – This method which branches the middleware pipeline based on the request path and invokes the appropriate delegate based on path provided.

Let’s take a bit deeper look into each of these extension methods.

Run

The signature of this method is as shown below:

image

This method configures a delegate which accept HttpContext as input and returns a Task object. It terminates the middleware pipeline by generating the response as shown below:

image

The output displayed in the console will be as expected:

image

The page will display a simple text output as shown below. This is because the middleware pipeline is terminated by this delegate and none of the MVC related components gets executed.

image

Use

The signature of this method is as shown below:

image

The code snippet below shows how the Use method can be used to chain the middleware components.

image

The output messages in the console will be in the following sequence:

image

Map

The method signature is as follows:

image

Then we have implemented two methods with IApplicationBuilder as input parameter as shown below:

image

The Map methods are called as shown below:

image

When I am accessing http://localhost:5000, the two request delegates added using the two Use methods and then the request delegate in the Run method is invoked.

image

When I am accessing http://localhost:5000/map1, the two request delegates added using the two Use methods and then the request delegate in the HandleMap1 method is invoked.

image

When I am accessing http://localhost:5000/map2 , the two request delegates added using the two Use methods and then the request delegate in the HandleMap2 method is invoked.

image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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