Closed
Description
Background and Motivation
This is the core method for adding middleware to the pipeline that we hid (unintentionally) on the WebApplication because it had an explicit implementation of IApplicationBuilder. Today this is possible but you need to cast to IApplictionBuilder first.
Proposed API
namespace Microsoft.AspNetCore.Builder
{
public static class WebApplication
{
+ public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);
}
}
Usage Examples
var app = WebApplication.Create();
app.Use(next =>
{
// Passthrough
return next;
});
Alternatives
var app = WebApplication.Create();
app.Use((context, next) =>
{
// Passthrough
return next(context);
});
The difference here is that there's no place to write the code that runs at startup. Middleware is broken into 2 phases:
- Construction of the middleware
- Execution of the request
app.Use(next =>
{
// This runs on middleware construction
return context =>
{
// This runs per request
return next(context);
};
});
Risks
None