Very candidacy before processing in PageLoaderMatcherPolicy (#16678)
Fixes https://github.com/aspnet/AspNetCore/issues/13996
This commit is contained in:
parent
da808ce9c3
commit
c11fe23f31
|
|
@ -65,6 +65,11 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
|
||||||
|
|
||||||
for (var i = 0; i < candidates.Count; i++)
|
for (var i = 0; i < candidates.Count; i++)
|
||||||
{
|
{
|
||||||
|
if (!candidates.IsValidCandidate(i))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
ref var candidate = ref candidates[i];
|
ref var candidate = ref candidates[i];
|
||||||
var endpoint = candidate.Endpoint;
|
var endpoint = candidate.Endpoint;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Testing;
|
||||||
|
using RoutingWebSite;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
|
|
@ -14,12 +16,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
{
|
{
|
||||||
public RoutingDynamicTest(MvcTestFixture<RoutingWebSite.StartupForDynamic> fixture)
|
public RoutingDynamicTest(MvcTestFixture<RoutingWebSite.StartupForDynamic> fixture)
|
||||||
{
|
{
|
||||||
var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder);
|
Factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(ConfigureWebHostBuilder);
|
||||||
Client = factory.CreateDefaultClient();
|
Client = Factory.CreateDefaultClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => builder.UseStartup<RoutingWebSite.StartupForDynamic>();
|
private static void ConfigureWebHostBuilder(IWebHostBuilder builder) => builder.UseStartup<RoutingWebSite.StartupForDynamic>();
|
||||||
|
|
||||||
|
public WebApplicationFactory<StartupForDynamic> Factory { get; }
|
||||||
public HttpClient Client { get; }
|
public HttpClient Client { get; }
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -99,5 +102,39 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
Assert.Equal("Hello from dynamic page: /DynamicPage", content);
|
Assert.Equal("Hello from dynamic page: /DynamicPage", content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AppWithDynamicRouteAndMapRazorPages_CanRouteToRazorPage()
|
||||||
|
{
|
||||||
|
// Regression test for https://github.com/aspnet/AspNetCore/issues/13996
|
||||||
|
// Arrange
|
||||||
|
var client = Factory.WithWebHostBuilder(b => b.UseStartup<StartupForDynamicAndRazorPages>()).CreateDefaultClient();
|
||||||
|
var url = "/PageWithLinks";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.GetAsync(url);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var document = await response.GetHtmlDocumentAsync();
|
||||||
|
var editLink = document.RequiredQuerySelector("#editlink");
|
||||||
|
Assert.Equal("/Edit/10", editLink.GetAttribute("href"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task AppWithDynamicRouteAndMapRazorPages_CanRouteToDynamicController()
|
||||||
|
{
|
||||||
|
// Regression test for https://github.com/aspnet/AspNetCore/issues/13996
|
||||||
|
// Arrange
|
||||||
|
var client = Factory.WithWebHostBuilder(b => b.UseStartup<StartupForDynamicAndRazorPages>()).CreateDefaultClient();
|
||||||
|
var url = "/de/area%3Dadmin,controller%3Ddynamic,action%3Dindex";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.GetAsync(url);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
|
||||||
|
var content = await response.Content.ReadAsStringAsync();
|
||||||
|
Assert.StartsWith("Hello from dynamic controller", content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
// 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.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Routing;
|
||||||
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace RoutingWebSite
|
||||||
|
{
|
||||||
|
// For by tests for a mix of dynamic routing + Razor Pages
|
||||||
|
public class StartupForDynamicAndRazorPages
|
||||||
|
{
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services
|
||||||
|
.AddMvc()
|
||||||
|
.SetCompatibilityVersion(CompatibilityVersion.Latest);
|
||||||
|
|
||||||
|
services.AddSingleton<Transformer>();
|
||||||
|
|
||||||
|
// Used by some controllers defined in this project.
|
||||||
|
services.Configure<RouteOptions>(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
app.UseRouting();
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapRazorPages();
|
||||||
|
endpoints.MapDynamicControllerRoute<Transformer>("{language}/{**slug}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Transformer : DynamicRouteValueTransformer
|
||||||
|
{
|
||||||
|
// Turns a format like `controller=Home,action=Index` into an RVD
|
||||||
|
public override ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
|
||||||
|
{
|
||||||
|
if (!(values["slug"] is string slug))
|
||||||
|
{
|
||||||
|
return new ValueTask<RouteValueDictionary>(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
var kvps = slug.Split(",");
|
||||||
|
|
||||||
|
var results = new RouteValueDictionary();
|
||||||
|
foreach (var kvp in kvps)
|
||||||
|
{
|
||||||
|
var split = kvp.Split("=");
|
||||||
|
results[split[0]] = split[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ValueTask<RouteValueDictionary>(results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue