
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use of Map Extension in ASP.NET Core Middleware Pipeline
Middleware are software components that are assembled into an application pipeline to handle requests and responses.
Each component chooses whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component is invoked in the pipeline.
Map extensions are used as convention for branching the pipeline.
The Map extension method is used to match request delegates based on a request’s path. Map simply accepts a path and a function that configures a separate middleware pipeline.
In the following example, any request with the base path of /maptest will be handled by the pipeline configured in the HandleMapTest method.
Example
private static void HandleMapTest(IApplicationBuilder app){ app.Run(async context =>{ await context.Response.WriteAsync("Map Test Successful"); }); } public void ConfigureMapping(IApplicationBuilder app){ app.Map("/maptest", HandleMapTest); }
In addition to path-based mapping, the MapWhen method supports predicate-based middleware branching, allowing separate pipelines to be constructed in a very flexible fashion.
Any predicate of type Func<HttpContext, bool> can be used to map requests to a new branch of the pipeline.
private static void HandleBranch(IApplicationBuilder app){ app.Run(async context =>{ await context.Response.WriteAsync("Branch used."); }); } public void ConfigureMapWhen(IApplicationBuilder app){ app.MapWhen(context => { return context.Request.Query.ContainsKey("branch"); }, HandleBranch); app.Run(async context =>{ await context.Response.WriteAsync("Hello from " + _environment); }); }
Maps can also be nested
app.Map("/level1", level1App => { level1App.Map("/level2a", level2AApp => { // "/level1/level2a" //... }); level1App.Map("/level2b", level2BApp => { // "/level1/level2b" //... }); });