From 9daf5ff7a4e092b3a84c060c44148d7d5d40b5c3 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 10 Oct 2018 18:33:30 +1300 Subject: [PATCH] Fix PageRouteTransformerConvention with custom page routes (#8576) --- .../Infrastructure/PageActionDescriptorProvider.cs | 6 ++++-- .../RoutingTestsBase.cs | 12 ++++++++++++ .../RoutingWebSite/Pages/LGAnotherPage.cshtml | 2 -- test/WebSites/RoutingWebSite/Pages/LGPage.cshtml | 2 -- .../Pages/PageRouteTransformer/Index.cshtml | 2 -- .../PageWithConfiguredRoute.cshtml | 2 ++ .../Pages/PageRouteTransformer/TestPage.cshtml | 2 -- test/WebSites/RoutingWebSite/Startup.cs | 1 + test/WebSites/RoutingWebSite/StartupWith21Compat.cs | 1 + 9 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/PageWithConfiguredRoute.cshtml diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs index 71a826c857..806a032c35 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageActionDescriptorProvider.cs @@ -129,8 +129,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure var pageRouteMetadata = selectorModel.EndpointMetadata.OfType().SingleOrDefault(); if (pageRouteMetadata == null) { - // Selector does not have expected metadata. Should never reach here - throw new InvalidOperationException("Page selector did not have page route metadata."); + // Selector does not have expected metadata + // This selector was likely configured by AddPageRouteModelConvention + // Use the existing explicitly configured template + return selectorModel.AttributeRouteModel.Template; } var segments = pageRouteMetadata.PageRoute.Split('/'); diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTestsBase.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTestsBase.cs index f842256023..6ee37de064 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTestsBase.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/RoutingTestsBase.cs @@ -147,6 +147,18 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests Assert.Equal("Hello from World", body); } + [Fact] + public async Task Page_PageRouteTransformer_PageWithConfiguredRoute() + { + // Arrange & Act + var response = await Client.GetAsync("http://localhost/PageRouteTransformer/NewConventionRoute/World"); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var body = await response.Content.ReadAsStringAsync(); + Assert.Equal("Hello from World", body); + } + [Fact] public virtual async Task ConventionalRoutedController_ActionIsReachable() { diff --git a/test/WebSites/RoutingWebSite/Pages/LGAnotherPage.cshtml b/test/WebSites/RoutingWebSite/Pages/LGAnotherPage.cshtml index d9a0e02177..ab3d7f487c 100644 --- a/test/WebSites/RoutingWebSite/Pages/LGAnotherPage.cshtml +++ b/test/WebSites/RoutingWebSite/Pages/LGAnotherPage.cshtml @@ -1,4 +1,2 @@ @page "{id?}" @model RoutingWebSite.Pages.LGAnotherPageModel -@{ -} diff --git a/test/WebSites/RoutingWebSite/Pages/LGPage.cshtml b/test/WebSites/RoutingWebSite/Pages/LGPage.cshtml index 598b475d1c..f7a24f6983 100644 --- a/test/WebSites/RoutingWebSite/Pages/LGPage.cshtml +++ b/test/WebSites/RoutingWebSite/Pages/LGPage.cshtml @@ -1,4 +1,2 @@ @page "{id?}" @model BasicWebSite.Pages.LGPageModel -@{ -} diff --git a/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/Index.cshtml b/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/Index.cshtml index dd7e49caa8..010bf8732a 100644 --- a/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/Index.cshtml +++ b/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/Index.cshtml @@ -1,3 +1 @@ @page -@{ -} diff --git a/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/PageWithConfiguredRoute.cshtml b/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/PageWithConfiguredRoute.cshtml new file mode 100644 index 0000000000..f03e3a98e4 --- /dev/null +++ b/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/PageWithConfiguredRoute.cshtml @@ -0,0 +1,2 @@ +@page "ExtraPath/{id?}" +Hello from @ViewContext.RouteData.Values["id"] \ No newline at end of file diff --git a/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/TestPage.cshtml b/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/TestPage.cshtml index 5e812c4640..f03e3a98e4 100644 --- a/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/TestPage.cshtml +++ b/test/WebSites/RoutingWebSite/Pages/PageRouteTransformer/TestPage.cshtml @@ -1,4 +1,2 @@ @page "ExtraPath/{id?}" -@{ -} Hello from @ViewContext.RouteData.Values["id"] \ No newline at end of file diff --git a/test/WebSites/RoutingWebSite/Startup.cs b/test/WebSites/RoutingWebSite/Startup.cs index 2b3a78b8e6..03b1fa2ba2 100644 --- a/test/WebSites/RoutingWebSite/Startup.cs +++ b/test/WebSites/RoutingWebSite/Startup.cs @@ -28,6 +28,7 @@ namespace RoutingWebSite }) .AddRazorPagesOptions(options => { + options.Conventions.AddPageRoute("/PageRouteTransformer/PageWithConfiguredRoute", "/PageRouteTransformer/NewConventionRoute/{id?}"); options.Conventions.AddFolderRouteModelConvention("/PageRouteTransformer", model => { pageRouteTransformerConvention.Apply(model); diff --git a/test/WebSites/RoutingWebSite/StartupWith21Compat.cs b/test/WebSites/RoutingWebSite/StartupWith21Compat.cs index 20800ec3ea..473653df24 100644 --- a/test/WebSites/RoutingWebSite/StartupWith21Compat.cs +++ b/test/WebSites/RoutingWebSite/StartupWith21Compat.cs @@ -26,6 +26,7 @@ namespace RoutingWebSite .AddMvc() .AddRazorPagesOptions(options => { + options.Conventions.AddPageRoute("/PageRouteTransformer/PageWithConfiguredRoute", "/PageRouteTransformer/NewConventionRoute/{id?}"); options.Conventions.AddFolderRouteModelConvention("/PageRouteTransformer", model => { pageRouteTransformerConvention.Apply(model);