UseExtensions.Use Method

Definition

Overloads

Use(IApplicationBuilder, Func<HttpContext,RequestDelegate,Task>)

Adds a middleware delegate defined in-line to the application's request pipeline. If you aren't calling the next function, use Run(IApplicationBuilder, RequestDelegate) instead.

Use(IApplicationBuilder, Func<HttpContext,Func<Task>,Task>)

Adds a middleware delegate defined in-line to the application's request pipeline. If you aren't calling the next function, use Run(IApplicationBuilder, RequestDelegate) instead.

Prefer using Use(IApplicationBuilder, Func<HttpContext,RequestDelegate,Task>) for better performance as shown below:

app.Use((context, next) =>
{
    return next(context);
});

Use(IApplicationBuilder, Func<HttpContext,RequestDelegate,Task>)

Source:
UseExtensions.cs

Adds a middleware delegate defined in-line to the application's request pipeline. If you aren't calling the next function, use Run(IApplicationBuilder, RequestDelegate) instead.

C#
public static Microsoft.AspNetCore.Builder.IApplicationBuilder Use (this Microsoft.AspNetCore.Builder.IApplicationBuilder app, Func<Microsoft.AspNetCore.Http.HttpContext,Microsoft.AspNetCore.Http.RequestDelegate,System.Threading.Tasks.Task> middleware);

Parameters

middleware
Func<HttpContext,RequestDelegate,Task>

A function that handles the request and calls the given next function.

Returns

The IApplicationBuilder instance.

Applies to

ASP.NET Core 9.0 and other versions
Product Versions
ASP.NET Core 6.0, 7.0, 8.0, 9.0

Use(IApplicationBuilder, Func<HttpContext,Func<Task>,Task>)

Source:
UseExtensions.cs
Source:
UseExtensions.cs
Source:
UseExtensions.cs

Adds a middleware delegate defined in-line to the application's request pipeline. If you aren't calling the next function, use Run(IApplicationBuilder, RequestDelegate) instead.

Prefer using Use(IApplicationBuilder, Func<HttpContext,RequestDelegate,Task>) for better performance as shown below:

app.Use((context, next) =>
{
    return next(context);
});

C#
public static Microsoft.AspNetCore.Builder.IApplicationBuilder Use (this Microsoft.AspNetCore.Builder.IApplicationBuilder app, Func<Microsoft.AspNetCore.Http.HttpContext,Func<System.Threading.Tasks.Task>,System.Threading.Tasks.Task> middleware);

Parameters

middleware
Func<HttpContext,Func<Task>,Task>

A function that handles the request and calls the given next function.

Returns

The IApplicationBuilder instance.

Applies to

ASP.NET Core 9.0 and other versions
Product Versions
ASP.NET Core 1.0, 1.1, 2.0, 2.1, 2.2, 3.0, 3.1, 5.0, 6.0, 7.0, 8.0, 9.0