HI WELCOME TO SIRIS

Map Extension Method in ASP.NET Core Application

Leave a Comment

In this article, 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 article 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 article.

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.

Map Extension Method in ASP.NET Core Application

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:

  1. app: The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
  2. pathMatch: The request path to match.
  3. configuration: The branch to take for positive path matches.
  4. 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.

private void MapCustomMiddleware(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Specific URL Logic Middleware \n");
});
}

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.

app.Map(“/testmap”, MapCustomMiddleware);

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.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace MiddlewareInASPNETCore
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Use Middleware Component \n");
await next();
});
app.Map("/testmap", MapCustomMiddleware);
app.Run(async context => {
await context.Response.WriteAsync("Run Middleware Component\n");
});
}
private void MapCustomMiddleware(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Specific URL Logic Middleware using Map Method \n");
});
}
}
}

Now, save the changes and run the application. First, check without the custom path as shown in the below image.

Example to Understand the Map Extension Method in ASP.NET Core

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.

Map Extension Method in ASP.NET Core Application with Examples

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 article, I am going to discuss How to Create, Register, and Use Custom Middleware Component in ASP.NET Core Application. Here, in this article, 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 article.