Where to Find ASP.NET Core Razor Pages Source Code: A Complete Guide to the Implementation
The ASP.NET Core Razor Pages source code is located in the dotnet/aspnetcore repository under src/Mvc/Mvc.RazorPages/src, containing the core logic for page discovery, routing, and view rendering.
The Razor Pages framework is implemented as a dedicated library within the ASP.NET Core MVC ecosystem. If you are debugging routing issues, customizing page conventions, or contributing to the framework, understanding the exact location of the ASP.NET Core Razor Pages source code helps you trace how application configuration translates into endpoint behavior.
Core Implementation Location
The Razor Pages implementation resides in the Mvc.RazorPages project within the main ASP.NET Core repository. This project contains the complete stack that handles page file discovery, endpoint mapping, and Razor view engine integration.
Project Structure
Navigate to the following directory to explore the source:
src/Mvc/Mvc.RazorPages/src/
This directory contains the fundamental classes that power the Razor Pages programming model. The implementation is organized into subfolders like Builder/ for routing extensions and DependencyInjection/ for service registration logic.
Key Components of the Razor Pages Stack
The Razor Pages framework consists of several coordinated components that handle configuration, routing, and view location. Each component has a specific responsibility within the request pipeline.
Configuration and Options
RazorPagesOptions ([RazorPagesOptions.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.RazorPages/src/RazorPagesOptions.cs)) serves as the central configuration holder. It stores settings such as the page root directory, area root directory, and the collection of page conventions. The RootDirectory property defaults to /Pages and determines where the runtime searches for .cshtml files.
The RazorPagesOptionsSetup class ([RazorPagesOptionsSetup.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.RazorPages/src/DependencyInjection/RazorPagesOptionsSetup.cs)) registers these options with the dependency injection container during application startup, ensuring the configuration is available throughout the framework.
Routing and Endpoint Mapping
RazorPagesEndpointRouteBuilderExtensions ([RazorPagesEndpointRouteBuilderExtensions.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.RazorPages/src/Builder/RazorPagesEndpointRouteBuilderExtensions.cs)) provides the extension methods that wire Razor Pages into the endpoint routing system. The MapRazorPages() method creates an endpoint data source that scans for page files and builds route patterns based on file system conventions.
This file also contains MapFallbackToPage(), which enables single-page application (SPA) scenarios by serving a specific page when no other endpoint matches the request.
View Engine Integration
RazorPagesRazorViewEngineOptionsSetup ([RazorPagesRazorViewEngineOptionsSetup.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.RazorPages/src/DependencyInjection/RazorPagesRazorViewEngineOptionsSetup.cs)) configures the Razor view engine location formats. It tells the view engine where to search for page templates and shared views, ensuring that Page() method calls resolve to the correct .cshtml files in the pages directory.
Page Conventions
PageConventionCollection ([PageConventionCollection.cs](https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.RazorPages/src/Builder/PageConventionCollection.cs)) stores the conventions applied to page models during application model construction. These conventions allow you to modify filters, route templates, and other model metadata globally or for specific pages.
Practical Implementation Examples
The following examples demonstrate how the source code components work together in a typical application.
Basic Setup and Routing
In Program.cs, the AddRazorPages() method registers all necessary services, including RazorPagesOptions and view engine configuration:
var builder = WebApplication.CreateBuilder(args);
// Registers RazorPagesOptions, view engine setup, and conventions
builder.Services.AddRazorPages();
var app = builder.Build();
// Uses RazorPagesEndpointRouteBuilderExtensions.MapRazorPages()
app.MapRazorPages();
app.Run();
Customizing Page Options
You can modify the configuration stored in RazorPagesOptions to change the root directory or add security conventions:
builder.Services.Configure<RazorPagesOptions>(options =>
{
// Changes the default folder from /Pages to /MyPages
options.RootDirectory = "/MyPages";
// Applies a filter to specific pages using conventions
options.Conventions.AddPageApplicationModelConvention(
"/Index",
model => model.Filters.Add(new AuthorizeFilter()));
});
Fallback Page Configuration
For scenarios where client-side routing handles navigation, use the fallback page extension:
// Serves Pages/_Host.cshtml when no API or MVC endpoint matches
app.MapFallbackToPage("/_Host");
This method leverages the same endpoint routing infrastructure defined in RazorPagesEndpointRouteBuilderExtensions to register a low-priority endpoint that serves the specified page.
Summary
- ASP.NET Core Razor Pages source code lives in
dotnet/aspnetcore/src/Mvc/Mvc.RazorPages/src/. RazorPagesOptions.cscontains the configuration settings for page discovery and root directories.RazorPagesEndpointRouteBuilderExtensions.csimplementsMapRazorPages()andMapFallbackToPage()for endpoint routing.RazorPagesRazorViewEngineOptionsSetup.csconfigures view location formats so the runtime finds.cshtmlfiles.PageConventionCollectionmanages the conventions that modify page models during startup.
Frequently Asked Questions
Where is the MapRazorPages method implemented?
The MapRazorPages method is implemented in RazorPagesEndpointRouteBuilderExtensions.cs within the src/Mvc/Mvc.RazorPages/src/Builder/ directory. This extension method on IEndpointRouteBuilder creates a PageActionEndpointConventionBuilder that scans for page files and registers them as endpoints in the routing system.
How does Razor Pages change the default pages folder?
The default folder is controlled by the RootDirectory property in RazorPagesOptions. You can modify this in Program.cs using the Configure<RazorPagesOptions> method. The source code for this configuration resides in RazorPagesOptions.cs, which validates that the directory starts with a forward slash and exists within the application root.
What handles the view engine locations for Razor Pages?
The RazorPagesRazorViewEngineOptionsSetup class configures the view engine search locations. Located in src/Mvc/Mvc.RazorPages/src/DependencyInjection/, this class sets up location formats such as /Pages/{0}.cshtml and /Pages/Shared/{0}.cshtml, ensuring the Razor view engine can resolve page views when you call Page() or return PageResult from a page model.
Are Razor Pages separate from MVC in the source code?
While Razor Pages share the MVC infrastructure, they are implemented in a separate project (Mvc.RazorPages) from the core MVC controllers (Mvc.Core). The Razor Pages project depends on the Razor view engine and MVC abstractions but provides its own page model discovery, routing conventions, and endpoint data sources specifically optimized for the page-based programming model.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →