Does Blazor Server 7 need to call MapRazorPages()?

David Thielen 2,421 Reputation points

When Visual Studio creates a Blazor server app, there's no call to app.MapRazorPages() in Program.cs

But I do see that call in a lot of examples. Is that something that used to be needed? Or should it be added back in?

I am using Asp.Net Core 7, Blazor server side.

thanks - dave

A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,451 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,531 Reputation points Microsoft Vendor

    Hi,@David Thielen,

    all your razor components were rendered on _Host.cshtml by default

    the document related

    • _Host.cshtml: The root page of the app implemented as a Razor Page:
    • When any page of the app is initially requested, this page is rendered and returned in the response.
    • The Host page specifies where the root App component (App.razor) is rendered.

    And app.MapFallbackToPage("/_Host"); is required to map different paths to _Host.cshtml

    Then ,you could check the source codes here

    public static PageActionEndpointConventionBuilder MapRazorPages(this IEndpointRouteBuilder endpoints)
            return GetOrCreateDataSource(endpoints).DefaultBuilder;
    public static IEndpointConventionBuilder MapFallbackToPage(this IEndpointRouteBuilder endpoints, string page)
            PageConventionCollection.EnsureValidPageName(page, nameof(page));
            // Called for side-effect to make sure that the data source is registered.
            var pageDataSource = GetOrCreateDataSource(endpoints);
            pageDataSource.CreateInertEndpoints = true;
            RegisterInCache(endpoints.ServiceProvider, pageDataSource);
            // 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));
                b.Metadata.Add(new PageEndpointDataSourceIdMetadata(pageDataSource.DataSourceId));
            return builder;

    The two extension methods both call EnsureRazorPagesServices(endpoints); to handle razor page endpoints.

    So you could visit razor page endpoints in default Blazor Server project even if app.MapRazorPages() is not called

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Ruikai Feng

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce ( 59,051 Reputation points

    You only need it if your site includes razor pages, say you use the identity package or add your own razor pages.