Use, Run and Map and in .NetCore


In .NET Core, the Use, Run and Map methods are part of the middleware pipeline in an ASP.NET Core application. Middleware is a way to handle HTTP requests and responses in a pipeline, enabling features such as authentication, routing, logging, and more.

Middleware plays a vital role in handling and processing HTTP requests within a .NET Core application's request pipeline. It enables developers to customize and extend the application's behavior. In this post, we will delve into three crucial functions used for configuring middleware: Use, Run, and Map.

Here's how they differ and when to use each.

 

1. Use Method

Use() method is used to insert a new middleware in the pipeline. Add middleware to the pipeline that can process requests and pass them to the next middleware in the chain. Has the ability to call the next middleware in the pipeline using the next() delegate. When you want to perform some processing (e.g., logging, authentication checks) and then pass control to the next middleware.

Next Method

The next () method is used to pass the execution to the next middleware. We will use the next method to call the next middleware.

Add the following code in program.cs file.

app.Use(async (context, next) =>
{
    // Before the next middleware
    Console.WriteLine("Before next middleware");
    
    await next(); // Call the next middleware

    // After the next middleware
    Console.WriteLine("After next middleware");
});

Output

Use, Run and Map in .NetCore

If we see other methods in Program.cs file use Use methods. The Use Extension Method in ASP.NET Core Application allows us to add a new middleware component which may call the next middleware component in the request processing pipeline. Here in Program.cs file exection will be seqentionaly because they all use USE Method.

Use, Run and Map in .NetCore

2. Run Method

Run() method is used to complete the middleware execution. Define terminal middleware that does not call the next middleware in the pipeline. Ends the pipeline. no further middleware is executed after this. When you need to handle a request completely and return a response directly.

app.Run(async context =>
{
    await context.Response.WriteAsync("This is the end of the pipeline.");
});

Output

Use, Run and Map in .NetCore

It is used in last Program.cs file.

Use, Run and Map in .NetCore

3. Map Method

Branch the middleware pipeline based on the request path. Creates a new pipeline for specific paths. When you want to handle requests differently based on their paths.

app.Map("/branch", branchApp =>
{
    branchApp.Run(async context =>
    {
        await context.Response.WriteAsync("You are in the /branch path.");
    });
});

In the above example /branch should be match with http://localhost:5067/branch Then it will execute and Output will be:

You are in the /branch path.

Practical Example

Here’s a full example of using all three methods in a single app.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.Use(async (context, next) =>
{
    Console.WriteLine("Middleware 1 - Before");
    await next();
    Console.WriteLine("Middleware 1 - After");
});

app.Map("/api", apiApp =>
{
    apiApp.Use(async (context, next) =>
    {
        Console.WriteLine("API Middleware");
        await next();
    });

    apiApp.Run(async context =>
    {
        await context.Response.WriteAsync("Welcome to the API!");
    });
});

app.Run(async context =>
{
    await context.Response.WriteAsync("Hello from the root!");
});

app.Run();

Prev Next

Top Articles

  1. What is JSON
  2. How to convert a javaScript object in JSON object
  3. Some Important JSON Examples
  4. Common JSON Interview Question