首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blazor应用程序如何能够在不调用MapRazorPages()的情况下在Startup.cs中公开Razor页面端点?

Blazor应用程序如何能够在不调用MapRazorPages()的情况下在Startup.cs中公开Razor页面端点?
EN

Stack Overflow用户
提问于 2020-01-19 00:29:59
回答 1查看 1K关注 0票数 1

在默认的MapRazorPages项目模板中,Startup.cs中启用MapRazorPages的部分代码是在配置()的端点配置部分调用MapRazorPages():

代码语言:javascript
复制
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页面中,端点初始化结果如下所示:

代码语言:javascript
复制
app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });

得到的应用程序能够正确地将请求路由到Razor页面端点,如Login.cshtml和LogOut.cshtml。如果不调用MapRazorPages(),这怎么可能呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-19 11:27:23

首先,看看下面的代码片段(请注意这两个代码段中突出显示的行)

参考MapRazorPages

代码语言:javascript
复制
/// <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;
}

参考MapFallbackToPage

代码语言:javascript
复制
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;
}

它们都调用核心函数,该函数将功能配置为处理剃须刀页面请求。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59806119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档