How to combine Minimal API’s Pipeline Branching with MapGet syntax?

Say I want to have 2 endpoints, /a and /b, while also utilizing the pipeline branching for both of those endpoints, for adding custom middleware for them.

The following code does not work:

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

var a = "/a";
var b = "/b";

app.Map(a, true, branch =>
{
    branch.Use((context, next) =>
    {
        context.Response.Headers.Add("X-Test", "a");
        return next(context);
    });
});

app.Map(b, true, branch =>
{
    branch.Use((context, next) =>
    {
        context.Response.Headers.Add("X-Test", "b");
        return next(context);
    });
});

app.MapGet(a, () => "hello from a!");
app.MapGet(b, () => "hello from b!");

app.Run();

Calling /a fails with:

System.InvalidOperationException: The request reached the end of the pipeline without executing the endpoint: 'HTTP: GET /a'. Please register the EndpointMiddleware using 'IApplicationBuilder.UseEndpoints(...)' if using routing.
   at Microsoft.AspNetCore.Builder.ApplicationBuilder.<>c.<Build>b__25_0(HttpContext context)
   at Program.<>c.<<Main>$>b__0_4(HttpContext context, RequestDelegate next) in C:Users...RiderProjectsMinimalApiSampleMinimalApiSampleProgram.cs:line 15
   at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

But I can’t figure out, where to put UseEndpoints and why do I need it after adding custom app.Map(... calls? I’m also not putting UseRouting anywhere because I tried it right before the first app.Map(... but it didn’t change the outcome and apparently it’s not required in Minimal API at all.

Use UseWhen:

Conditionally creates a branch in the request pipeline that is rejoined to the main pipeline.

app.UseWhen(context => context.Request.Path == a, ab => ab.Use((context, next) =>
{
    context.Response.Headers.Add("X-Test", "a");
    return next(context);
}));

//... the same for the b

app.MapGet(a, () => "hello from a!");

Map on the other hand creates just a separate branch:

Branches the request pipeline based on matches of the given request path. If the request path starts with the given path, the branch is executed.

Also see the Branch the middleware pipeline section of the docs.

UPD

if you need the separate branch then you can just build it fully. For example for /b:

app.Map(b, true, branch =>
{
    branch.Use((context, next) =>
    {
        context.Response.Headers.Add("X-Test", "b");
        return next(context);
    });
    branch.UseRouting();
    branch.UseEndpoints(rb => rb.MapGet(b, () => "hello from b!"));
});

7

You can use MapWhen. Below is your example with applied corrections:

app.MapWhen(context => context.Request.Path == a, branch =>
{
    branch.Run(context =>
    {
        context.Response.Headers.Add("X-Test", "a");
        return Task.CompletedTask;
    });
});

app.MapWhen(context => context.Request.Path == b, branch =>
{
    branch.Run(context =>
    {
        context.Response.Headers.Add("X-Test", "b");
        return Task.CompletedTask;
    });
});

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật