liveBook · Manning

AddRazorPagesOptions

As explained in ASP.NET Core in Action, 3e

AddRazorPagesOptions is an ASP.NET Core extension method that configures Razor Pages options and conventions during service registration.

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.

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains chaining, route transformers, extra routes, root-directory changes, and custom conventions, but not convention internals.
Examples
Uses kebab-case transformation, an alternative search-products route, and a prefixing page-route convention.
What it assumes
Assumes a Razor Pages application with AddRazorPages() and Program.cs service registration already in place.
What you can do after
Operate/configure Razor Pages routing by registering transformers, additional page routes, and custom route conventions.
How it connects
Connects Razor Pages with routing conventions, parameter transformers, AddPageRoute, RouteOptions, and IPageRouteModelConvention.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.