AddRazorPagesOptions is an ASP.NET Core extension method for customizing Razor Pages conventions during service registration. Chain it after AddRazorPages() in Program.cs, then configure the RazorPagesOptions object. section 14.6 section 14.7
Use the options callback to change how Razor Pages routes are generated or matched:
- Call AddPageRoute to add an alternative route for one page. The additional template supplements rather than replaces the page's default route. section 14.6
- Add a custom IPageRouteModelConvention when the application needs broader control over route templates. section 14.6
- Set RootDirectory inside the configuration lambda to customize the Pages root folder convention. section 14.6
Register a transformer and an extra route
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages() .AddRazorPagesOptions(opts => { opts.Conventions.Add( new PageRouteTransformerConvention( new KebabCaseParameterTransformer())); opts.Conventions.AddPageRoute( "/Search/Products/StartSearch", "/search-products"); }); WebApplication app = builder.Build(); app.MapRazorPages(); app.Run(); This applies the parameter transformer to Razor Pages and lets the specified page respond through both its normal route and /search-products.
