- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of "Map" extension while adding middleware to C# ASP.NET Core 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" //... }); });
- Related Articles
- How C# ASP.NET Core Middleware is different from HttpModule?
- How to handle errors in middleware C# Asp.net Core?
- What is the use of UseIISIntegration in C# Asp.net Core?
- What is Kestral C# Asp.net Core?
- What is the use of the Configure() method of startup class in C# Asp.net Core?
- What is routing in C# ASP.NET Core?
- What is Metapackage in C# Asp.net Core?
- What is the role of IWebHostEnvironment interface in C# ASP.NET Core?
- What are the benefits of choosing ASP.NET Core over ASP.NET?
- What is the purpose of Program.cs file in C# ASP.NET Core project?
- What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?
- What is the AddSingleton vs AddScoped vs Add Transient C# Asp.net Core?
- What is ASP.NET Core? Explain how it's different from the ASP.NET framework.
- How to enable Session in C# ASP.NET Core?
- What are the various JSON files available in C# ASP.NET Core?
