Understanding Middleware in ASP.NET Core – Part 2

In this section, we are going to delve further. Here, we will focus on other methods available to us like

  • Map
  • MapWhen

Let’s start with Map. Map has two parameters. Below is the concrete structure for the same.

12th

Map has two parameters. First is pathMatch and second is delegate named configuration. Map allows you to add additional middleware components in the pipeline and this will be executed if it matches the string provided in the pathMatch, then it will start the requested path. Below is the complete structure of Map.

13th

Below is the glimpse of Map extension. Here, I have provided “/myview” as path and just printed one text there using Run Middleware. Therefore, here you can see how branching is happening. In the Use extension, I have also printed one line after invoke method. This is there, just to prove the point that how context to and fro travels in the middleware pipeline.

14th

Let’s run and see the same in action. If there comes a base request, then it will produce below output.

15th

Notice, last line; this came from the first middleware after context is returning from terminal middleware. Now, let’s go ahead and provide the map. In this case, it will produce the following output.

16th

One thing to notice here, it didn’t go outside Map. Here is the thing, if you are using Map extension, it won’t land in terminal middleware outside Map branch. It will automatically terminate that. It won’t even need Run inside Map to terminate the pipeline. I have used here, just to print the message. Let me comment the same. Now my code looks like

17th

With the above changes in place, when I go ahead and Run the same, it will produce the following output.

18th

It means, it printed first line from 1st middleware, which is Use extension, then it invoked next middleware which is Map extension, where it matched the path. Upon successful match, it just terminated the pipeline and returned the result in browser. Now, let’s go ahead and see the structure of MapWhen.

19th

Here first parameter is the predicate and second parameter is the same configuration. Let’s go ahead and see the same in action.

20th

With the above change in place, when I go ahead and run the app, and apply querystring like shown below, then it will produce the following thing.

21th

Similarly, If I don’t keep terminal middleware inside our MapWhen extension, then also it will get terminated and below is the result for the same.

22nd

18thI can also go ahead and use middleware inside middleware like shown below.

24th

In the above use case, I have used Use extension inside MapWhen. Upon running the same, it will produce the following output.

25th

Output is pretty predictable and understood. It first executed first middleware, then came in next middleware, where it matched the request, then invoked next middleware, which printed 3rd line and finally while returning it printed the last line. However, if I don’t invoke the next middleware from MapWhen extension like shown below, then output would be something like this.

26th

27th

Below is the finished code for the same.

I hope you guys have liked this discussion around Middleware chaining. In the coming section, will delve further around the same. Till then stay tuned and Happy Coding.

Thanks,
Rahul Sahay
Happy Coding