diff --git a/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs b/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs index 47938611d6..1da0918d91 100644 --- a/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs +++ b/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs @@ -5,20 +5,13 @@ namespace Microsoft.AspNetCore.Builder { public static partial class EndpointRouteBuilderExtensions { - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, Microsoft.AspNetCore.Routing.Patterns.RoutePattern pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, Microsoft.AspNetCore.Routing.Patterns.RoutePattern pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapDelete(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapDelete(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapGet(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapGet(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapPost(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapPost(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapPut(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapPut(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapVerbs(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, System.Collections.Generic.IList httpMethods, params object[] metadata) { throw null; } - public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapVerbs(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, string displayName, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate, System.Collections.Generic.IList httpMethods, params object[] metadata) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, Microsoft.AspNetCore.Routing.Patterns.RoutePattern pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapDelete(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapGet(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapMethods(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, System.Collections.Generic.IEnumerable httpMethods, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapPost(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder MapPut(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes, string pattern, Microsoft.AspNetCore.Http.RequestDelegate requestDelegate) { throw null; } } public static partial class EndpointRoutingApplicationBuilderExtensions { @@ -51,6 +44,9 @@ namespace Microsoft.AspNetCore.Builder public static partial class RoutingEndpointConventionBuilderExtensions { public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder RequireHost(this Microsoft.AspNetCore.Builder.IEndpointConventionBuilder builder, params string[] hosts) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder WithDisplayName(this Microsoft.AspNetCore.Builder.IEndpointConventionBuilder builder, System.Func func) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder WithDisplayName(this Microsoft.AspNetCore.Builder.IEndpointConventionBuilder builder, string displayName) { throw null; } + public static Microsoft.AspNetCore.Builder.IEndpointConventionBuilder WithMetadata(this Microsoft.AspNetCore.Builder.IEndpointConventionBuilder builder, params object[] items) { throw null; } } } namespace Microsoft.AspNetCore.Routing diff --git a/src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs index 716be5af9f..9d8c8957bd 100644 --- a/src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs @@ -29,35 +29,13 @@ namespace Microsoft.AspNetCore.Builder /// The to add the route to. /// The route pattern. /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. public static IEndpointConventionBuilder MapGet( this IEndpointRouteBuilder routes, string pattern, - RequestDelegate requestDelegate, - params object[] metadata) + RequestDelegate requestDelegate) { - return MapVerbs(routes, pattern, displayName: null, requestDelegate, GetVerb, metadata); - } - - /// - /// Adds a to the that matches HTTP GET requests - /// for the specified pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder MapGet( - this IEndpointRouteBuilder routes, - string pattern, - string displayName, - RequestDelegate requestDelegate, - params object[] metadata) - { - return MapVerbs(routes, pattern, displayName, requestDelegate, GetVerb, metadata); + return MapMethods(routes, pattern, GetVerb, requestDelegate); } /// @@ -67,35 +45,13 @@ namespace Microsoft.AspNetCore.Builder /// The to add the route to. /// The route pattern. /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. public static IEndpointConventionBuilder MapPost( this IEndpointRouteBuilder routes, string pattern, - RequestDelegate requestDelegate, - params object[] metadata) + RequestDelegate requestDelegate) { - return MapVerbs(routes, pattern, displayName: null, requestDelegate, PostVerb, metadata); - } - - /// - /// Adds a to the that matches HTTP POST requests - /// for the specified pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder MapPost( - this IEndpointRouteBuilder routes, - string pattern, - string displayName, - RequestDelegate requestDelegate, - params object[] metadata) - { - return MapVerbs(routes, pattern, displayName, requestDelegate, PostVerb, metadata); + return MapMethods(routes, pattern, PostVerb, requestDelegate); } /// @@ -105,35 +61,13 @@ namespace Microsoft.AspNetCore.Builder /// The to add the route to. /// The route pattern. /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. public static IEndpointConventionBuilder MapPut( this IEndpointRouteBuilder routes, string pattern, - RequestDelegate requestDelegate, - params object[] metadata) + RequestDelegate requestDelegate) { - return MapVerbs(routes, pattern, displayName: null, requestDelegate, PutVerb, metadata); - } - - /// - /// Adds a to the that matches HTTP PUT requests - /// for the specified pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder MapPut( - this IEndpointRouteBuilder routes, - string pattern, - string displayName, - RequestDelegate requestDelegate, - params object[] metadata) - { - return MapVerbs(routes, pattern, displayName, requestDelegate, PutVerb, metadata); + return MapMethods(routes, pattern, PutVerb, requestDelegate); } /// @@ -143,35 +77,13 @@ namespace Microsoft.AspNetCore.Builder /// The to add the route to. /// The route pattern. /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. public static IEndpointConventionBuilder MapDelete( this IEndpointRouteBuilder routes, string pattern, - RequestDelegate requestDelegate, - params object[] metadata) + RequestDelegate requestDelegate) { - return MapVerbs(routes, pattern, displayName: null, requestDelegate, DeleteVerb, metadata); - } - - /// - /// Adds a to the that matches HTTP DELETE requests - /// for the specified pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder MapDelete( - this IEndpointRouteBuilder routes, - string pattern, - string displayName, - RequestDelegate requestDelegate, - params object[] metadata) - { - return MapVerbs(routes, pattern, displayName, requestDelegate, DeleteVerb, metadata); + return MapMethods(routes, pattern, DeleteVerb, requestDelegate); } /// @@ -182,50 +94,22 @@ namespace Microsoft.AspNetCore.Builder /// The route pattern. /// The delegate executed when the endpoint is matched. /// HTTP methods that the endpoint will match. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder MapVerbs( + public static IEndpointConventionBuilder MapMethods( this IEndpointRouteBuilder routes, string pattern, - RequestDelegate requestDelegate, - IList httpMethods, - params object[] metadata) - { - return MapVerbs(routes, pattern, displayName: null, requestDelegate, httpMethods, metadata); - } - - /// - /// Adds a to the that matches HTTP requests - /// for the specified HTTP methods and pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// HTTP methods that the endpoint will match. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder MapVerbs( - this IEndpointRouteBuilder routes, - string pattern, - string displayName, - RequestDelegate requestDelegate, - IList httpMethods, - params object[] metadata) + IEnumerable httpMethods, + RequestDelegate requestDelegate) { if (httpMethods == null) { throw new ArgumentNullException(nameof(httpMethods)); } - var resolvedMetadata = new List(); - resolvedMetadata.Add(new HttpMethodMetadata(httpMethods)); - if (metadata != null) - { - resolvedMetadata.AddRange(metadata); - } - - return Map(routes, pattern, displayName ?? $"{pattern} HTTP: {string.Join(", ", httpMethods)}", requestDelegate, metadata: resolvedMetadata.ToArray()); + var builder = routes.Map(RoutePatternFactory.Parse(pattern), requestDelegate); + builder.WithDisplayName($"{pattern} HTTP: {string.Join(", ", httpMethods)}"); + builder.WithMetadata(new HttpMethodMetadata(httpMethods)); + return builder; } /// @@ -235,35 +119,13 @@ namespace Microsoft.AspNetCore.Builder /// The to add the route to. /// The route pattern. /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. public static IEndpointConventionBuilder Map( this IEndpointRouteBuilder routes, string pattern, - RequestDelegate requestDelegate, - params object[] metadata) + RequestDelegate requestDelegate) { - return Map(routes, RoutePatternFactory.Parse(pattern), pattern, requestDelegate, metadata); - } - - /// - /// Adds a to the that matches HTTP requests - /// for the specified pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder Map( - this IEndpointRouteBuilder routes, - string pattern, - string displayName, - RequestDelegate requestDelegate, - params object[] metadata) - { - return Map(routes, RoutePatternFactory.Parse(pattern), displayName, requestDelegate, metadata); + return Map(routes, RoutePatternFactory.Parse(pattern), requestDelegate); } /// @@ -273,33 +135,11 @@ namespace Microsoft.AspNetCore.Builder /// The to add the route to. /// The route pattern. /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. /// A that can be used to further customize the endpoint. public static IEndpointConventionBuilder Map( this IEndpointRouteBuilder routes, RoutePattern pattern, - RequestDelegate requestDelegate, - params object[] metadata) - { - return Map(routes, pattern, pattern.RawText ?? pattern.DebuggerToString(), requestDelegate, metadata); - } - - /// - /// Adds a to the that matches HTTP requests - /// for the specified pattern. - /// - /// The to add the route to. - /// The route pattern. - /// The display name for the endpoint. - /// The delegate executed when the endpoint is matched. - /// Metadata that is added to the endpoint. - /// A that can be used to further customize the endpoint. - public static IEndpointConventionBuilder Map( - this IEndpointRouteBuilder routes, - RoutePattern pattern, - string displayName, - RequestDelegate requestDelegate, - params object[] metadata) + RequestDelegate requestDelegate) { if (routes == null) { @@ -318,12 +158,12 @@ namespace Microsoft.AspNetCore.Builder const int defaultOrder = 0; - var routeEndpointBuilder = new RouteEndpointBuilder( + var builder = new RouteEndpointBuilder( requestDelegate, pattern, defaultOrder) { - DisplayName = displayName + DisplayName = pattern.RawText ?? pattern.DebuggerToString(), }; // Add delegate attributes as metadata @@ -334,27 +174,18 @@ namespace Microsoft.AspNetCore.Builder { foreach (var attribute in attributes) { - routeEndpointBuilder.Metadata.Add(attribute); + builder.Metadata.Add(attribute); } } - if (metadata != null) + var dataSource = routes.DataSources.OfType().FirstOrDefault(); + if (dataSource == null) { - foreach (var item in metadata) - { - routeEndpointBuilder.Metadata.Add(item); - } + dataSource = new ModelEndpointDataSource(); + routes.DataSources.Add(dataSource); } - var modelEndpointDataSource = routes.DataSources.OfType().FirstOrDefault(); - - if (modelEndpointDataSource == null) - { - modelEndpointDataSource = new ModelEndpointDataSource(); - routes.DataSources.Add(modelEndpointDataSource); - } - - return modelEndpointDataSource.AddEndpointBuilder(routeEndpointBuilder); + return dataSource.AddEndpointBuilder(builder); } } } diff --git a/src/Http/Routing/src/Builder/FallbackEndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/FallbackEndpointRouteBuilderExtensions.cs index 561f8f6608..f034aeb873 100644 --- a/src/Http/Routing/src/Builder/FallbackEndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/FallbackEndpointRouteBuilderExtensions.cs @@ -87,7 +87,8 @@ namespace Microsoft.AspNetCore.Builder throw new ArgumentNullException(nameof(requestDelegate)); } - var conventionBuilder = routes.Map(pattern, "Fallback " + pattern, requestDelegate); + var conventionBuilder = routes.Map(pattern, requestDelegate); + conventionBuilder.WithDisplayName("Fallback " + pattern); conventionBuilder.Add(b => ((RouteEndpointBuilder)b).Order = int.MaxValue); return conventionBuilder; } diff --git a/src/Http/Routing/src/Builder/RoutingEndpointConventionBuilderExtensions.cs b/src/Http/Routing/src/Builder/RoutingEndpointConventionBuilderExtensions.cs index 5aaf7498e7..fbc9589bd8 100644 --- a/src/Http/Routing/src/Builder/RoutingEndpointConventionBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/RoutingEndpointConventionBuilderExtensions.cs @@ -39,5 +39,84 @@ namespace Microsoft.AspNetCore.Builder }); return builder; } + + /// + /// Sets the to the provided for all + /// builders created by . + /// + /// The . + /// The display name. + /// The . + public static IEndpointConventionBuilder WithDisplayName(this IEndpointConventionBuilder builder, string displayName) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + builder.Add(b => + { + b.DisplayName = displayName; + }); + + return builder; + } + + /// + /// Sets the using the provided for all + /// builders created by . + /// + /// The . + /// A delegate that produces the display name for each . + /// The . + public static IEndpointConventionBuilder WithDisplayName(this IEndpointConventionBuilder builder, Func func) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (func == null) + { + throw new ArgumentNullException(nameof(func)); + } + + builder.Add(b => + { + b.DisplayName = func(b); + }); + + return builder; + } + + /// + /// Adds the provided metadata to for all builders + /// produced by . + /// + /// The . + /// A collection of metadata items. + /// The . + public static IEndpointConventionBuilder WithMetadata(this IEndpointConventionBuilder builder, params object[] items) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (items == null) + { + throw new ArgumentNullException(nameof(items)); + } + + builder.Add(b => + { + foreach (var item in items) + { + b.Metadata.Add(item); + } + }); + + return builder; + } } } diff --git a/src/Http/Routing/src/DefaultEndpointConventionBuilder.cs b/src/Http/Routing/src/DefaultEndpointConventionBuilder.cs new file mode 100644 index 0000000000..7c2e96f7f4 --- /dev/null +++ b/src/Http/Routing/src/DefaultEndpointConventionBuilder.cs @@ -0,0 +1,38 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; + +namespace Microsoft.AspNetCore.Routing +{ + internal class DefaultEndpointConventionBuilder : IEndpointConventionBuilder + { + internal EndpointBuilder EndpointBuilder { get; } + + private readonly List> _conventions; + + public DefaultEndpointConventionBuilder(EndpointBuilder endpointBuilder) + { + EndpointBuilder = endpointBuilder; + _conventions = new List>(); + } + + public void Add(Action convention) + { + _conventions.Add(convention); + } + + public Endpoint Build() + { + foreach (var convention in _conventions) + { + convention(EndpointBuilder); + } + + return EndpointBuilder.Build(); + } + } +} diff --git a/src/Http/Routing/src/ModelEndpointDataSource.cs b/src/Http/Routing/src/ModelEndpointDataSource.cs index 3585641b98..c9212c457b 100644 --- a/src/Http/Routing/src/ModelEndpointDataSource.cs +++ b/src/Http/Routing/src/ModelEndpointDataSource.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Builder; @@ -13,16 +12,16 @@ namespace Microsoft.AspNetCore.Routing { internal class ModelEndpointDataSource : EndpointDataSource { - private List _endpointConventionBuilders; + private List _endpointConventionBuilders; public ModelEndpointDataSource() { - _endpointConventionBuilders = new List(); + _endpointConventionBuilders = new List(); } public IEndpointConventionBuilder AddEndpointBuilder(EndpointBuilder endpointBuilder) { - var builder = new EndpointConventionBuilder(endpointBuilder); + var builder = new DefaultEndpointConventionBuilder(endpointBuilder); _endpointConventionBuilders.Add(builder); return builder; @@ -37,33 +36,5 @@ namespace Microsoft.AspNetCore.Routing // for testing internal IEnumerable EndpointBuilders => _endpointConventionBuilders.Select(b => b.EndpointBuilder); - - private class EndpointConventionBuilder : IEndpointConventionBuilder - { - internal EndpointBuilder EndpointBuilder { get; } - - private readonly List> _conventions; - - public EndpointConventionBuilder(EndpointBuilder endpointBuilder) - { - EndpointBuilder = endpointBuilder; - _conventions = new List>(); - } - - public void Add(Action convention) - { - _conventions.Add(convention); - } - - public Endpoint Build() - { - foreach (var convention in _conventions) - { - convention(EndpointBuilder); - } - - return EndpointBuilder.Build(); - } - } } } diff --git a/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs b/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs index c264d004f1..804af033a6 100644 --- a/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs +++ b/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs @@ -167,14 +167,14 @@ namespace Microsoft.AspNetCore.Builder // Act app.UseRouting(builder => { - builder.Map("/1", "Test endpoint 1", d => null); - builder.Map("/2", "Test endpoint 2", d => null); + builder.Map("/1", d => null).WithDisplayName("Test endpoint 1"); + builder.Map("/2", d => null).WithDisplayName("Test endpoint 2"); }); app.UseRouting(builder => { - builder.Map("/3", "Test endpoint 3", d => null); - builder.Map("/4", "Test endpoint 4", d => null); + builder.Map("/3", d => null).WithDisplayName("Test endpoint 3"); + builder.Map("/4", d => null).WithDisplayName("Test endpoint 4"); }); // This triggers the middleware to be created and the matcher factory to be called diff --git a/src/Http/Routing/test/UnitTests/Builder/MapEndpointEndpointDataSourceBuilderExtensionsTest.cs b/src/Http/Routing/test/UnitTests/Builder/MapEndpointEndpointDataSourceBuilderExtensionsTest.cs index 3227c83b46..e68d1a0bfc 100644 --- a/src/Http/Routing/test/UnitTests/Builder/MapEndpointEndpointDataSourceBuilderExtensionsTest.cs +++ b/src/Http/Routing/test/UnitTests/Builder/MapEndpointEndpointDataSourceBuilderExtensionsTest.cs @@ -33,13 +33,13 @@ namespace Microsoft.AspNetCore.Builder RequestDelegate requestDelegate = (d) => null; // Act - var endpointBuilder = builder.Map("/", "Display name!", requestDelegate); + var endpointBuilder = builder.Map("/", requestDelegate); // Assert var endpointBuilder1 = GetRouteEndpointBuilder(builder); Assert.Equal(requestDelegate, endpointBuilder1.RequestDelegate); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); + Assert.Equal("/", endpointBuilder1.DisplayName); Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); } @@ -51,54 +51,16 @@ namespace Microsoft.AspNetCore.Builder RequestDelegate requestDelegate = (d) => null; // Act - var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), "Display name!", requestDelegate); + var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), requestDelegate); // Assert var endpointBuilder1 = GetRouteEndpointBuilder(builder); Assert.Equal(requestDelegate, endpointBuilder1.RequestDelegate); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); + Assert.Equal("/", endpointBuilder1.DisplayName); Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); } - [Fact] - public void MapEndpoint_StringPatternAndMetadata_BuildsEndpoint() - { - // Arrange - var metadata = new object(); - var builder = new DefaultEndpointRouteBuilder(); - RequestDelegate requestDelegate = (d) => null; - - // Act - var endpointBuilder = builder.Map("/", "Display name!", requestDelegate, new[] { metadata }); - - // Assert - var endpointBuilder1 = GetRouteEndpointBuilder(builder); - Assert.Equal(requestDelegate, endpointBuilder1.RequestDelegate); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); - Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); - Assert.Equal(metadata, Assert.Single(endpointBuilder1.Metadata)); - } - - [Fact] - public void MapEndpoint_TypedPatternAndMetadata_BuildsEndpoint() - { - // Arrange - var metadata = new object(); - var builder = new DefaultEndpointRouteBuilder(); - RequestDelegate requestDelegate = (d) => null; - - // Act - var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), "Display name!", requestDelegate, new[] { metadata }); - - // Assert - var endpointBuilder1 = GetRouteEndpointBuilder(builder); - Assert.Equal(requestDelegate, endpointBuilder1.RequestDelegate); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); - Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); - Assert.Equal(metadata, Assert.Single(endpointBuilder1.Metadata)); - } - [Fact] public void MapEndpoint_AttributesCollectedAsMetadata() { @@ -106,36 +68,16 @@ namespace Microsoft.AspNetCore.Builder var builder = new DefaultEndpointRouteBuilder(); // Act - var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), "Display name!", Handle); + var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), Handle); // Assert var endpointBuilder1 = GetRouteEndpointBuilder(builder); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); Assert.Equal(2, endpointBuilder1.Metadata.Count); Assert.IsType(endpointBuilder1.Metadata[0]); Assert.IsType(endpointBuilder1.Metadata[1]); } - [Fact] - public void MapEndpoint_ExplicitMetadataAddedAfterAttributeMetadata() - { - // Arrange - var builder = new DefaultEndpointRouteBuilder(); - - // Act - var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), "Display name!", Handle, new Metadata()); - - // Assert - var endpointBuilder1 = GetRouteEndpointBuilder(builder); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); - Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); - Assert.Equal(3, endpointBuilder1.Metadata.Count); - Assert.IsType(endpointBuilder1.Metadata[0]); - Assert.IsType(endpointBuilder1.Metadata[1]); - Assert.IsType(endpointBuilder1.Metadata[2]); - } - [Fact] public void MapEndpoint_GeneratedDelegateWorks() { @@ -145,14 +87,11 @@ namespace Microsoft.AspNetCore.Builder Expression handler = context => Task.CompletedTask; // Act - var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), "Display name!", handler.Compile(), new Metadata()); + var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), handler.Compile()); // Assert var endpointBuilder1 = GetRouteEndpointBuilder(builder); - Assert.Equal("Display name!", endpointBuilder1.DisplayName); Assert.Equal("/", endpointBuilder1.RoutePattern.RawText); - Assert.Equal(1, endpointBuilder1.Metadata.Count); - Assert.IsType(endpointBuilder1.Metadata[0]); } [Fact] @@ -162,21 +101,17 @@ namespace Microsoft.AspNetCore.Builder var builder = new DefaultEndpointRouteBuilder(); // Act - var endpointBuilder = builder.MapGet("/", HandleHttpMetdata, new HttpMethodMetadata(new[] { "METHOD" })); - endpointBuilder.Add(b => - { - b.Metadata.Add(new HttpMethodMetadata(new[] { "BUILDER" })); - }); + var endpointBuilder = builder.MapMethods("/", new[] { "METHOD" }, HandleHttpMetdata); + endpointBuilder.WithMetadata(new HttpMethodMetadata(new[] { "BUILDER" })); // Assert var dataSource = Assert.Single(builder.DataSources); var endpoint = Assert.Single(dataSource.Endpoints); - Assert.Equal(4, endpoint.Metadata.Count); + Assert.Equal(3, endpoint.Metadata.Count); Assert.Equal("ATTRIBUTE", GetMethod(endpoint.Metadata[0])); - Assert.Equal("GET", GetMethod(endpoint.Metadata[1])); - Assert.Equal("METHOD", GetMethod(endpoint.Metadata[2])); - Assert.Equal("BUILDER", GetMethod(endpoint.Metadata[3])); + Assert.Equal("METHOD", GetMethod(endpoint.Metadata[1])); + Assert.Equal("BUILDER", GetMethod(endpoint.Metadata[2])); Assert.Equal("BUILDER", endpoint.Metadata.GetMetadata().HttpMethods.Single()); diff --git a/src/Http/Routing/test/UnitTests/Builder/RoutingEndpointConventionBuilderExtensionsTest.cs b/src/Http/Routing/test/UnitTests/Builder/RoutingEndpointConventionBuilderExtensionsTest.cs new file mode 100644 index 0000000000..ce7f9d2b3e --- /dev/null +++ b/src/Http/Routing/test/UnitTests/Builder/RoutingEndpointConventionBuilderExtensionsTest.cs @@ -0,0 +1,85 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.Routing.Patterns; +using Xunit; + +namespace Microsoft.AspNetCore.Builder +{ + public class RoutingEndpointConventionBuilderExtensionsTest + { + [Fact] + public void RequireHost_AddsHostMetadata() + { + // Arrange + var builder = CreateBuilder(); + + // Act + builder.RequireHost("www.example.com", "example.com"); + + // Assert + var endpoint = builder.Build(); + + var metadata = endpoint.Metadata.GetMetadata(); + Assert.NotNull(metadata); + Assert.Equal(new[] { "www.example.com", "example.com" }, metadata.Hosts); + } + + [Fact] + public void WithDisplayName_String_SetsDisplayName() + { + // Arrange + var builder = CreateBuilder(); + + // Act + builder.WithDisplayName("test"); + + // Assert + var endpoint = builder.Build(); + Assert.Equal("test", endpoint.DisplayName); + } + + [Fact] + public void WithDisplayName_Func_SetsDisplayName() + { + // Arrange + var builder = CreateBuilder(); + + // Act + builder.WithDisplayName(b => "test"); + + // Assert + var endpoint = builder.Build(); + Assert.Equal("test", endpoint.DisplayName); + } + + [Fact] + public void WithMetadata_AddsMetadata() + { + // Arrange + var builder = CreateBuilder(); + + // Act + builder.WithMetadata("test", new HostAttribute("www.example.com", "example.com")); + + // Assert + var endpoint = builder.Build(); + + var hosts = endpoint.Metadata.GetMetadata(); + Assert.NotNull(hosts); + Assert.Equal(new[] { "www.example.com", "example.com" }, hosts.Hosts); + + var @string = endpoint.Metadata.GetMetadata(); + Assert.Equal("test", @string); + } + + private DefaultEndpointConventionBuilder CreateBuilder() + { + return new DefaultEndpointConventionBuilder(new RouteEndpointBuilder( + TestConstants.EmptyRequestDelegate, + RoutePatternFactory.Parse("/test"), + order: 0)); + } + } +} diff --git a/src/Http/Routing/test/testassets/RoutingSandbox/HelloExtension/EndpointRouteBuilderExtensions.cs b/src/Http/Routing/test/testassets/RoutingSandbox/HelloExtension/EndpointRouteBuilderExtensions.cs index ac19c714b6..d08ac2d3a5 100644 --- a/src/Http/Routing/test/testassets/RoutingSandbox/HelloExtension/EndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/test/testassets/RoutingSandbox/HelloExtension/EndpointRouteBuilderExtensions.cs @@ -24,10 +24,7 @@ namespace Microsoft.AspNetCore.Builder .UseHello(greeter) .Build(); - return routes.Map( - pattern, - "Hello " + greeter, - pipeline); + return routes.Map(pattern, pipeline); } } } diff --git a/src/Http/Routing/test/testassets/RoutingSandbox/UseEndpointRoutingStartup.cs b/src/Http/Routing/test/testassets/RoutingSandbox/UseEndpointRoutingStartup.cs index 681eae941d..8a7740f4f1 100644 --- a/src/Http/Routing/test/testassets/RoutingSandbox/UseEndpointRoutingStartup.cs +++ b/src/Http/Routing/test/testassets/RoutingSandbox/UseEndpointRoutingStartup.cs @@ -71,7 +71,6 @@ namespace RoutingSandbox }); builder.MapGet( "/graph", - "DFA Graph", (httpContext) => { using (var writer = new StreamWriter(httpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true)) @@ -82,7 +81,7 @@ namespace RoutingSandbox } return Task.CompletedTask; - }); + }).WithDisplayName("DFA Graph"); builder.MapGet("/attributes", HandlerWithAttributes); diff --git a/src/Http/Routing/test/testassets/RoutingWebSite/HelloExtension/EndpointRouteBuilderExtensions.cs b/src/Http/Routing/test/testassets/RoutingWebSite/HelloExtension/EndpointRouteBuilderExtensions.cs index 14ca404a34..8d146cd4b1 100644 --- a/src/Http/Routing/test/testassets/RoutingWebSite/HelloExtension/EndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/test/testassets/RoutingWebSite/HelloExtension/EndpointRouteBuilderExtensions.cs @@ -24,10 +24,7 @@ namespace Microsoft.AspNetCore.Builder .UseHello(greeter) .Build(); - return routes.Map( - template, - "Hello " + greeter, - pipeline); + return routes.Map(template, pipeline).WithDisplayName("Hello " + greeter); } } } diff --git a/src/Http/Routing/test/testassets/RoutingWebSite/UseEndpointRoutingStartup.cs b/src/Http/Routing/test/testassets/RoutingWebSite/UseEndpointRoutingStartup.cs index 37c679c745..904a49fa1f 100644 --- a/src/Http/Routing/test/testassets/RoutingWebSite/UseEndpointRoutingStartup.cs +++ b/src/Http/Routing/test/testassets/RoutingWebSite/UseEndpointRoutingStartup.cs @@ -108,8 +108,7 @@ namespace RoutingWebSite response.ContentType = "text/plain"; return response.WriteAsync( "Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithSingleAsteriskCatchAll", new { })); - }, - new RouteNameMetadata(routeName: "WithSingleAsteriskCatchAll")); + }).WithMetadata(new RouteNameMetadata(routeName: "WithSingleAsteriskCatchAll")); routes.MapGet( "/WithDoubleAsteriskCatchAll/{**path}", (httpContext) => @@ -121,8 +120,7 @@ namespace RoutingWebSite response.ContentType = "text/plain"; return response.WriteAsync( "Link: " + linkGenerator.GetPathByRouteValues(httpContext, "WithDoubleAsteriskCatchAll", new { })); - }, - new RouteNameMetadata(routeName: "WithDoubleAsteriskCatchAll")); + }).WithMetadata(new RouteNameMetadata(routeName: "WithDoubleAsteriskCatchAll")); MapHostEndpoint(routes); MapHostEndpoint(routes, "*.0.0.1"); diff --git a/src/Mvc/samples/MvcSandbox/Startup.cs b/src/Mvc/samples/MvcSandbox/Startup.cs index f23c8de0c2..17962a48ec 100644 --- a/src/Mvc/samples/MvcSandbox/Startup.cs +++ b/src/Mvc/samples/MvcSandbox/Startup.cs @@ -38,8 +38,7 @@ namespace MvcSandbox { builder.MapGet( requestDelegate: WriteEndpoints, - pattern: "/endpoints", - displayName: "Home"); + pattern: "/endpoints").WithDisplayName("Home"); builder.MapControllerRoute( name: "default", @@ -53,7 +52,6 @@ namespace MvcSandbox builder.MapGet( "/graph", - "DFA Graph", (httpContext) => { using (var writer = new StreamWriter(httpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true)) @@ -64,7 +62,7 @@ namespace MvcSandbox } return Task.CompletedTask; - }); + }).WithDisplayName("DFA Graph"); builder.MapControllers(); builder.MapRazorPages();