In this Video, I am going to discuss how to work with the Map Extension Method in ASP.NET Core HTTP Request Processing Pipeline. Please read our previous Video where we discussed how to work with the Run, Next, and Use Extension methods in ASP.NET Core Application. We are also going to work with the same application that we created in our previous Video.
Map Extension Method in ASP.NET Core Application. The Complete ASP.NET Core Web API Developer Course 2023 [Videos].
Map Extension Method in ASP.NET Core
If you want to insert some specific middleware logic for some specific URL, then you can do the same using the Map Extension Method in any type of ASP.NET Core Application. Before using the Map Method, let us first have a look at the definition of this method which is shown in the below image. There are two overloaded versions available for this method in ASP.NET Core.
The Map method Branches the request pipeline based on matches of the given request path. If the request path starts with the given path, the branch is executed else the Middleware simply ignored. The Map Method takes the following Parameters:
- app: The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
- pathMatch: The request path to match.
- configuration: The branch to take for positive path matches.
- preserveMatchedPathSegment: if false, matched path would be removed from Request.Path and added to Request.PathBase.
Returns: The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
Example to Understand the Map Extension Method in ASP.NET Core:
Let us understand the Map Extension Method with Examples. First, add the following method in the Startup class. This is the custom logic that we want to execute for a specific URL.
Now, we need to register the Middleware component using the Map Extension method. As you can see here, we have provided the path as “/testmap” and also provided the second parameter as MapCustomMiddleware method that we just created.
With this when the request comes, if it contains /testmap as part of the URL, then only this Middleware component going to be executed else simply is ignored.
Complete code of Startup class:
The following is the complete code of the Startup class.
Now, save the changes and run the application. First, check without the custom path as shown in the below image.
As you can see in the above image, the Middleware which is configured using the Map Extension method will not be executed. This is because the incoming request URL does not include the path /testmap as part of the URL. Now, modify the URL to include /testmap as part of the URL and see the response as shown in the below image.
Now, you can see in the above, first, the first middleware executed and then the second middleware executed which is specifically designed for this request. In the next Video, I am going to discuss How to Create, Register, and Use Custom Middleware Component in ASP.NET Core Application. Here, in this Video, I try to explain the Map Extension Method in ASP.NET Core Application with Examples and I hope you enjoy this Map Extension Method in the ASP.NET Core Video.