如何合并对IEndpointRouteBuilder的多个委托调用?

问题描述

在我拥有的代码中的一处

app.UseEndpoints(endpoints => 
{
    endpoints.MapOneDefaultEndpoints()
    endpoints.MapAnotherDefaultEndpoint()
}

在我想要的创业公司中

app.UseEndpoints(endpoints => 
{
    endpoints.MapOneCustomEndpoint()
    endpoints.MapAnotherCustomEndpoint()
}

原因是我要隐藏认端点配置,以便仅需要配置自定义端点。有没有办法建立例如List<Action<IEndpointRouteBuilder>>,然后将其传递给认配置?

解决方法

所以我想我已经找到了一个解决方案,看起来像

public virtual void Configure(IApplicationBuilder app)
{
    var customEndpoints = new List<Action<IEndpointRouteBuilder>>
    {
        e => e.MapOneDefaultEndpoints(),e => e.MapAnotherDefaultEndpoint(),};

    app.UseCustomServices(customEndpoints: customEndpoints);
}

在我的UseCustomServices中,添加其余的端点

public static IApplicationBuilder UseCustomServices(this IApplicationBuilder app,List<Action<IEndpointRouteBuilder>> customEndpoints)
{
    customEndpoints.Add(e => e.MapOneDefaultEndpoints());
    customEndpoints.Add(e => e.MapAnotherCustomEndpoint());

    // Register whole list of endpoints
    app.UseEndpoints(endpoints =>
    {
        customEndpoints.ForEach(ce => ce(endpoints));
    });

    return app;
}