Skip to content

Commit 571bc25

Browse files
committed
Add support for global conventions on WebApplication
1 parent d90faf7 commit 571bc25

File tree

5 files changed

+379
-5
lines changed

5 files changed

+379
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Builder.WebApplication.Conventions.get -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder!

src/DefaultBuilder/src/WebApplication.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ public sealed class WebApplication : IHost, IApplicationBuilder, IEndpointRouteB
2727
internal const string GlobalEndpointRouteBuilderKey = "__GlobalEndpointRouteBuilder";
2828

2929
private readonly IHost _host;
30-
private readonly List<EndpointDataSource> _dataSources = new();
30+
private readonly GlobalEndpointRouteBuilder _globalEndpointRouteBuilder;
31+
private readonly RouteGroupBuilder _globalRouteGroupBuilder;
3132

3233
internal WebApplication(IHost host)
3334
{
3435
_host = host;
36+
37+
_globalEndpointRouteBuilder = new(this);
38+
_globalRouteGroupBuilder = _globalEndpointRouteBuilder.MapGroup(string.Empty);
39+
3540
ApplicationBuilder = new ApplicationBuilder(host.Services, ServerFeatures);
3641
Logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(Environment.ApplicationName ?? nameof(WebApplication));
3742

38-
Properties[GlobalEndpointRouteBuilderKey] = this;
43+
Properties[GlobalEndpointRouteBuilderKey] = _globalEndpointRouteBuilder;
3944
}
4045

4146
/// <summary>
@@ -80,9 +85,14 @@ IServiceProvider IApplicationBuilder.ApplicationServices
8085
internal IDictionary<string, object?> Properties => ApplicationBuilder.Properties;
8186
IDictionary<string, object?> IApplicationBuilder.Properties => Properties;
8287

83-
internal ICollection<EndpointDataSource> DataSources => _dataSources;
88+
internal ICollection<EndpointDataSource> DataSources => ((IEndpointRouteBuilder)_globalRouteGroupBuilder).DataSources;
8489
ICollection<EndpointDataSource> IEndpointRouteBuilder.DataSources => DataSources;
8590

91+
/// <summary>
92+
/// Gets the <see cref="IEndpointConventionBuilder"/> for the application.
93+
/// </summary>
94+
public IEndpointConventionBuilder Conventions => _globalRouteGroupBuilder;
95+
8696
internal ApplicationBuilder ApplicationBuilder { get; }
8797

8898
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
@@ -307,4 +317,13 @@ public IList<string>? Middleware
307317
}
308318
}
309319
}
320+
321+
private sealed class GlobalEndpointRouteBuilder(WebApplication application) : IEndpointRouteBuilder
322+
{
323+
public IServiceProvider ServiceProvider => application.Services;
324+
325+
public ICollection<EndpointDataSource> DataSources { get; } = [];
326+
327+
public IApplicationBuilder CreateApplicationBuilder() => ((IApplicationBuilder)application).New();
328+
}
310329
}

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,10 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
407407
// destination.Run(source)
408408
// destination.UseEndpoints()
409409

410-
// Set the route builder so that UseRouting will use the WebApplication as the IEndpointRouteBuilder for route matching
411-
app.Properties.Add(WebApplication.GlobalEndpointRouteBuilderKey, _builtApplication);
410+
// Set the route builder so that UseRouting will use the RouteGroupBuilder managed
411+
// by the WebApplication as the IEndpointRouteBuilder instance
412+
var globalRouteGroupBuilder = _builtApplication.Properties[WebApplication.GlobalEndpointRouteBuilderKey];
413+
app.Properties.Add(WebApplication.GlobalEndpointRouteBuilderKey, globalRouteGroupBuilder);
412414

413415
// Only call UseRouting() if there are endpoints configured and UseRouting() wasn't called on the global route builder already
414416
if (_builtApplication.DataSources.Count > 0)

src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/Microsoft.AspNetCore.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
<ItemGroup>
88
<Reference Include="Microsoft.AspNetCore" />
9+
<Reference Include="Microsoft.AspNetCore.SignalR" />
10+
<Reference Include="Microsoft.AspNetCore.Components.Endpoints" />
11+
<Reference Include="Microsoft.AspNetCore.Mvc" />
12+
<Reference Include="Microsoft.AspNetCore.Mvc.Core" />
913
<Reference Include="Microsoft.AspNetCore.TestHost" />
1014
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
1115
<Content Include="Microsoft.AspNetCore.TestHost.StaticWebAssets.xml" CopyToOutputDirectory="PreserveNewest" />

0 commit comments

Comments
 (0)