Skip to content
代码片段 群组 项目
未验证 提交 4076badb 编辑于 作者: Pranav K's avatar Pranav K 提交者: GitHub
浏览文件

Allow DefaultPageLoader.LoadAsync to work with CompiledPageActionDescriptor (#35325)

With a fallback page, DefaultPageLoader might observe a mix of PageActionDescriptor
and CompiledPageActionDescriptor instances. Currently encountering the latter results in an exception.

Fixes https://github.com/dotnet/aspnetcore/issues/35060
上级 fad96588
No related branches found
No related tags found
无相关合并请求
......@@ -45,6 +45,14 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
throw new ArgumentNullException(nameof(actionDescriptor));
}
if (actionDescriptor is CompiledPageActionDescriptor compiledPageActionDescriptor)
{
// It's possible for some code paths of PageLoaderMatcherPolicy to invoke LoadAsync with an instance
// of CompiledPageActionDescriptor. In that case, we'll return the instance as-is.
compiledPageActionDescriptor.CompiledPageActionDescriptorTask ??= Task.FromResult(compiledPageActionDescriptor);
return compiledPageActionDescriptor.CompiledPageActionDescriptorTask;
}
var task = actionDescriptor.CompiledPageActionDescriptorTask;
if (task != null)
......
......@@ -294,7 +294,6 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
},
};
var transformer = new Mock<RoutePatternTransformer>();
transformer
.Setup(t => t.SubstituteRequiredValues(It.IsAny<RoutePattern>(), It.IsAny<object>()))
......@@ -335,6 +334,29 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
Assert.NotSame(result1, result2);
}
[Fact]
public async Task LoadAsync_CompiledPageActionDescriptor_ReturnsSelf()
{
// Arrange
var mvcOptions = Options.Create(new MvcOptions());
var endpointFactory = new ActionEndpointFactory(Mock.Of<RoutePatternTransformer>(), Enumerable.Empty<IRequestDelegateFactory>());
var loader = new DefaultPageLoader(
new[] { Mock.Of<IPageApplicationModelProvider>() },
Mock.Of<IViewCompilerProvider>(),
endpointFactory,
RazorPagesOptions,
mvcOptions);
var pageDescriptor = new CompiledPageActionDescriptor();
// Act
var result1 = await loader.LoadAsync(pageDescriptor, new EndpointMetadataCollection());
var result2 = await loader.LoadAsync(pageDescriptor, new EndpointMetadataCollection());
// Assert
Assert.Same(pageDescriptor, result1);
Assert.Same(pageDescriptor, result2);
}
private static IViewCompilerProvider GetCompilerProvider()
{
var compiledItem = TestRazorCompiledItem.CreateForView(typeof(object), "/Views/Index.cshtml");
......
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
......@@ -50,6 +50,19 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
Assert.Equal("Hello from runtime-compiled rzc page!", responseBody.Trim());
}
[Fact]
public async Task RuntimeCompilation_WithFallbackPage_Works()
{
// Regression test for https://github.com/dotnet/aspnetcore/issues/35060
// Act
var response = await Client.GetAsync("Fallback");
// Assert
await response.AssertStatusCodeAsync(HttpStatusCode.OK);
var responseBody = await response.Content.ReadAsStringAsync();
Assert.Equal("Hello from fallback page!", responseBody.Trim());
}
[Fact]
public async Task Rzc_LocalViewWithDifferentContent_IsUsed()
{
......
@page
Hello from fallback page!
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
......@@ -27,6 +27,7 @@ namespace RazorBuildWebSite
{
endpoints.MapDefaultControllerRoute();
endpoints.MapRazorPages();
endpoints.MapFallbackToPage("/Fallback");
});
}
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册