Skip to content

Expose Use(Func<RequestDelegate, RequestDelegate> middleware) on WebApplication #37346

Closed
@davidfowl

Description

@davidfowl

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:

  1. Construction of the middleware
  2. Execution of the request
app.Use(next =>
{
    // This runs on middleware construction
    return context => 
    {
           // This runs per request
          return next(context);
    };
});

Risks

None

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-hosting

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions