[ASP.NET Core MVC Pipeline] Middleware Pipeline

The ASP.NET Core MVC Pipeline
The ASP.NET Core MVC Pipeline
Middleware Pipeline
Middleware Pipeline

Middleware is software that is 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.

Middleware Pipeline
Middleware Pipeline
public class Startup
{
// ... More Code

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage(); // Exception Handling Page Middleware

app.UseStaticFiles(); // Static Files serving Middleware

app.UseMvcWithDefaultRoute(); // MVC Routing Middleware
}
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage(); // Exception Handling Page Middleware

app.UseStaticFiles(); // Static Files serving Middleware

/* Custom Middleware Inline */
app.Use(async (context, next) =>
{
if (context.Request.Method == "GET")
await next();
else
await context.Response.WriteAsync("Invalid method");
});
/* End Custom Middleware Inline */

app.UseMvcWithDefaultRoute(); // MVC Routing Middleware
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();

app.Use(async (context, next) =>
{
// Execute some BEFORE before calling the next middleware
await next();
// Execute some AFTER before calling the next middleware
});

app.UseMvcWithDefaultRoute();
}
app.Use(async (context, next) => { 
// Our custom logic
if(something)
{
await next();
// More custom logic
}
else
{
// Next middleware is never called on this path
}
});
app.Map("/CustomPath", (appPath1) =>
{
appPath1.Use(async (context, next) =>
{
// Cuustom Middleware code for Path 1
await next();
});
});

app.Map("/AnotherCustomPath", (appPath2) =>
{
appPath2.Run(async (context) =>
{
// Custom Middleware code for Path 2
await context.Response.WriteAsync("Done");
});
});
app.MapWhen(context => context.Request.IsHttps, appContext =>
{
appContext.Use(async (context, next) =>
{
// Custom Logic
await next();
});
});

app.MapWhen(context => context.Request.Method == "GET", appContext =>
{
appContext.Run(async context =>
{
await context.Response.WriteAsync("Done!");
});
});
public class CustomHeadersMiddleware
{
private readonly RequestDelegate next;

public CustomHeadersMiddleware(RequestDelegate next)
{
this.next = next;
}

public Task Invoke(HttpContext context)
{
return next(context);
}
}
public class CustomHeadersMiddleware
{
private readonly RequestDelegate next;

public CustomHeadersMiddleware(RequestDelegate next)
{
this.next = next;
}

public async Task Invoke(HttpContext context)
{
if (context.Request.Method == "GET")
{
context.Response.Headers.Add("AzureCoderBlog", DateTime.UtcNow.ToString());
await next(context);
}
else
{
await context.Response.WriteAsync("Method not Allowed");
}
}
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();

app.UseMiddleware<CustomHeadersMiddleware>();

app.UseMvcWithDefaultRoute();
}
public static class CustomHeadersMiddlewareExtensions
{
public static IApplicationBuilder UseCustomHeaders(this IApplicationBuilder app)
{
return app.UseMiddleware<CustomHeadersMiddleware>();
}
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();

app.UseCustomHeaders();

app.UseMvcWithDefaultRoute();
}
Middleware Samples
Middleware Samples

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store