|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Linq; |
7 | 7 | using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.AspNetCore.Http.Features; |
8 | 9 | using Microsoft.AspNetCore.Routing.TestObjects; |
9 | 10 | using Microsoft.Extensions.DependencyInjection; |
10 | 11 | using Xunit; |
@@ -597,6 +598,64 @@ public void GetTemplateBinder_CanClearCache() |
597 | 598 | Assert.NotSame(original, actual); |
598 | 599 | } |
599 | 600 |
|
| 601 | + [Fact] |
| 602 | + public void GetPathByRouteValues_UsesFirstTemplateThatSucceeds() |
| 603 | + { |
| 604 | + // Arrange |
| 605 | + var endpointControllerAction = EndpointFactory.CreateRouteEndpoint( |
| 606 | + "Home/Index", |
| 607 | + order: 3, |
| 608 | + defaults: new { controller = "Home", action = "Index", }, |
| 609 | + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); |
| 610 | + var endpointController = EndpointFactory.CreateRouteEndpoint( |
| 611 | + "Home", |
| 612 | + order: 2, |
| 613 | + defaults: new { controller = "Home", action = "Index", }, |
| 614 | + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); |
| 615 | + var endpointEmpty = EndpointFactory.CreateRouteEndpoint( |
| 616 | + "", |
| 617 | + order: 1, |
| 618 | + defaults: new { controller = "Home", action = "Index", }, |
| 619 | + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); |
| 620 | + |
| 621 | + // This endpoint should be used to generate the link when an id is present |
| 622 | + var endpointControllerActionParameter = EndpointFactory.CreateRouteEndpoint( |
| 623 | + "Home/Index/{id}", |
| 624 | + order: 0, |
| 625 | + defaults: new { controller = "Home", action = "Index", }, |
| 626 | + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); |
| 627 | + |
| 628 | + var linkGenerator = CreateLinkGenerator(endpointControllerAction, endpointController, endpointEmpty, endpointControllerActionParameter); |
| 629 | + |
| 630 | + var context = new EndpointSelectorContext() |
| 631 | + { |
| 632 | + RouteValues = new RouteValueDictionary(new { controller = "Home", action = "Index", }) |
| 633 | + }; |
| 634 | + var httpContext = CreateHttpContext(); |
| 635 | + httpContext.Features.Set<IRouteValuesFeature>(context); |
| 636 | + |
| 637 | + // Act |
| 638 | + var pathWithoutId = linkGenerator.GetPathByRouteValues( |
| 639 | + httpContext, |
| 640 | + routeName: null, |
| 641 | + values: new RouteValueDictionary()); |
| 642 | + |
| 643 | + var pathWithId = linkGenerator.GetPathByRouteValues( |
| 644 | + httpContext, |
| 645 | + routeName: null, |
| 646 | + values: new RouteValueDictionary(new { id = "3" })); |
| 647 | + |
| 648 | + var pathWithCustom = linkGenerator.GetPathByRouteValues( |
| 649 | + httpContext, |
| 650 | + routeName: null, |
| 651 | + values: new RouteValueDictionary(new { custom = "Custom" })); |
| 652 | + |
| 653 | + // Assert |
| 654 | + Assert.Equal("/", pathWithoutId); |
| 655 | + Assert.Equal("/Home/Index/3", pathWithId); |
| 656 | + Assert.Equal("/?custom=Custom", pathWithCustom); |
| 657 | + } |
| 658 | + |
600 | 659 | protected override void AddAdditionalServices(IServiceCollection services) |
601 | 660 | { |
602 | 661 | services.AddSingleton<IEndpointAddressScheme<int>, IntAddressScheme>(); |
|
0 commit comments