In last article, we have seen what is a middleware and how the middlewares are configured in ASP .NET Core application pipeline. We discussed how to configure them using Run and Use extension methods.
In this article, let’s discuss about the third extension method – Map.
You can refer to my previous blog post about middleware components. Follow the steps to create the ASP .NET Core 5 web application. We are going to use same application further in this article.
Before we begin to know the “HOW” part, it would be great to understand the concept of why branching the application request pipeline might be required.
As you know, the Configure method from Startup class, defines the sequence of request delegates through which request would pass. In real world, there may be cases where you want different middleware components for subset of application requests.
You may want to serve the static files (scripts or images) without even checking authorization of the user, thereby reducing load on the server. While the admin module from the web application needs more checks (e.g. checking if application is being requested from internal users).
Now, let’s see how to branch the request processing pipeline.
The Map extension methods are used to branch the request processing pipeline. The Map extension method takes input path (complete or partial) and if the incoming request’s path matches, the new branch (second parameter of Map method) is executed.
Below is sample code showing how branching works.
Below points show how the request would be processed and how the pipeline branches.
- https://localhost/ or https://localhost/token would print The default pipeline is executed response.
- https://localhost/first or https://localhost/first/token would print the First branch is executed
- https://localhost/second or https://localhost/second/token would print the Second branch is executed
The request pipeline can also have nested branches. In above snippet, the Map method can be used to further branch the https://localhost/first branch.
Map is used, the matched path segments are removed from
HttpRequest.Path and appended to
HttpRequest.PathBase for each request.
The Map method can either take a single token from URL (like /first) or it can take multiple tokens together (like /first/level-2). If multiple tokens are specified together, then the request pipeline branches only if all of the specified tokens match.
Below code shows how the code snippet above can be further modified to form nested branches.
There might be real world scenarios when the application needs to branch the request processing pipeline based on certain conditions.
The MapWhen delegate can be used in such cases. It branches the request pipeline only if the predicate returns true. Below code snippet checks if there is a query string variable with name
token is present. If it is available, then the pipeline branches.
I hope you find this information helpful. Let me know your thoughts.
This Post Has 2 Comments
Appreciate very much for this detailed explanation.
Pingback: The Code Blogger - NuGet for unit testing ASP .NET Core middleware