在默认的MapRazorPages项目模板中,Startup.cs中启用MapRazorPages的部分代码是在配置()的端点配置部分调用MapRazorPages():
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});这个调用的必要性在Rick和Ryan的优秀ASP.NET内核中Razor页面简介文章中得到了证实。
虽然Blazor是一种不同的UI技术,但Blazor项目也能够公开Razor页面端点。例如,包含ASP.NET身份验证的Blazor项目将登录页和LogOut页面公开为Razor页面。
但是,暴露Razor页面的Blazor项目中的端点初始化似乎不涉及对MapRazorPages()的调用。如果使用带有单个用户帐户身份验证的默认Blazor模板创建一个新项目,那么在ASP.NET标识所使用的所有Razor页面中,端点初始化结果如下所示:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});得到的应用程序能够正确地将请求路由到Razor页面端点,如Login.cshtml和LogOut.cshtml。如果不调用MapRazorPages(),这怎么可能呢?
发布于 2020-01-19 11:27:23
首先,看看下面的代码片段(请注意这两个代码段中突出显示的行)
/// <summary>
/// Adds endpoints for Razor Pages to the <see cref="IEndpointRouteBuilder"/>.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <returns>An <see cref="PageActionEndpointConventionBuilder"/> for endpoints associated with Razor Pages.</returns>
public static PageActionEndpointConventionBuilder MapRazorPages(this IEndpointRouteBuilder endpoints)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}
EnsureRazorPagesServices(endpoints); //<-- NOTE THIS
return GetOrCreateDataSource(endpoints).DefaultBuilder;
}public static IEndpointConventionBuilder MapFallbackToPage(this IEndpointRouteBuilder endpoints, string page)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}
if (page == null)
{
throw new ArgumentNullException(nameof(page));
}
PageConventionCollection.EnsureValidPageName(page, nameof(page));
EnsureRazorPagesServices(endpoints); //<-- NOTE THIS
// Called for side-effect to make sure that the data source is registered.
GetOrCreateDataSource(endpoints).CreateInertEndpoints = true;
// Maps a fallback endpoint with an empty delegate. This is OK because
// we don't expect the delegate to run.
var builder = endpoints.MapFallback(context => Task.CompletedTask);
builder.Add(b =>
{
// MVC registers a policy that looks for this metadata.
b.Metadata.Add(CreateDynamicPageMetadata(page, area: null));
});
return builder;
}它们都调用核心函数,该函数将功能配置为处理剃须刀页面请求。
https://stackoverflow.com/questions/59806119
复制相似问题