The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
Middleware in ASP.NET Core is configured by adding it to the HTTP request pipeline inside the
Startup.Configure method (or in Program.cs for .NET 6 and later). This pipeline is built using the
IApplicationBuilder interface with a series of method calls like
UseMiddleware(), UseRouting(), UseAuthentication(), and so on.
Middleware components are executed in the order they are added.
You use .UseXyz() methods to add middleware that passes control to the next component.
.Run() can be used to terminate the pipeline early (not common except for very specific cases).
2. In Program.cs (Minimal Hosting Model — .NET 6 and later)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddControllers();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
3. Adding Custom Middleware
You can add your own middleware with:
app.Use(async (context, next) =>
{
// Code before next middleware runs
Console.WriteLine("Request Incoming");
await next(); // Call the next middleware
// Code after next middleware runs
Console.WriteLine("Response Outgoing");
});
Or create a class middleware and add it with:
app.UseMiddleware<MyCustomMiddleware>();
Summary
Middleware is configured by calling extension methods on IApplicationBuilder inside the
Configure method or the minimal hosting Program.cs.
Order matters: The order you add middleware affects how requests and responses flow.
Use .Use...() for middleware that calls the next one, .Run() for terminal middleware.
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Middleware in ASP.NET Core is configured by adding it to the HTTP request pipeline inside the
Startup.Configuremethod (or inProgram.csfor .NET 6 and later). This pipeline is built using theIApplicationBuilderinterface with a series of method calls likeUseMiddleware(),UseRouting(),UseAuthentication(), and so on.How to Configure Middleware — Step by Step
1. Inside
Startup.Configure(Pre .NET 6 style).UseXyz()methods to add middleware that passes control to the next component..Run()can be used to terminate the pipeline early (not common except for very specific cases).2. In
Program.cs(Minimal Hosting Model — .NET 6 and later)3. Adding Custom Middleware
You can add your own middleware with:
Or create a class middleware and add it with:
Summary
IApplicationBuilderinside theConfiguremethod or the minimal hostingProgram.cs..Use...()for middleware that calls the next one,.Run()for terminal middleware.