Create a file with the following content. You can name the file anything, but for explanation purposes, we'll call it WebPaths.cs.
WebPaths.cs
1 2 3 4 5 6 7 8 910111213141516
usingBlazorPathHelper;// The BlazorPath attribute enables automatic generation.// Also, make sure to declare the class as partial.[BlazorPath]publicpartialclassWebPaths{// Define URL pages as constants like this.// public const string (VariableName) = "/your-path";publicconststringIndex="/";publicconststringSample="/sample";publicconststringSampleChild="/sample/child";publicconststringCounter="/counter";publicconststringCounterWithState="/counter/{count:int}";publicconststringCounterWithQuery="/counter/query";}
When defined this way, the following class definition is automatically generated.
Automatically Generated URL Builder
Auto Generated Code
1 2 3 4 5 6 7 8 9101112131415
// <auto-generated />publicpartialclassWebPaths{// A URL builder is generated in the Helper classpublicpartialclassHelper{publicstaticstringIndex()=>"/";publicstaticstringSample()=>"/sample";publicstaticstringSampleChild()=>"/sample/child";publicstaticstringCounter()=>"/counter";publicstaticstringCounterWithState(intCount)=>string.Format("/counter/{0}",ToStringForUrl(Count));publicstaticstringCounterWithQuery()=>"/counter/query";}}
A URL builder that accepts an int parameter has been generated for WebPaths.Helper.CounterWithState. However, CounterWithQuery is not yet set up to accept queries because the query parameters have not been defined yet.
usingBlazorPathHelper;[BlazorPath]publicpartialclassWebPaths{publicconststringIndex="/";publicconststringSample="/sample";publicconststringSampleChild="/sample/child";publicconststringCounter="/counter";publicconststringCounterWithState="/counter/{count:int}";[Query<QueryRecord>]// Specify the Query attribute like thispublicconststringCounterWithQuery="/counter/query";}// Define a class to accept query parameters.// This class must be written in a .cs file. (See the note below)// It is recommended to specify default values or make parameters nullable.publicrecordQueryRecord(stringquery="hello",intpage=0,bool?opt=null);
Note!
The class definition for QueryRecord above will not be generated correctly if written in a .razor file. This is due to the nature of source generators. (Razor files are also converted to C# by source generators, which can cause conflicts.)
This will automatically generate the following class definition.
Automatically Generated URL Query Builder
Auto Generated Code
1 2 3 4 5 6 7 8 91011121314
// <auto-generated />publicpartialclassWebPaths{publicpartialclassHelper{// Common parts are omittedpublicstaticstringCounterWithQuery(QueryRecord__query)=>string.Format("/counter/query{0}",BuildQuery([ToEscapedStrings("query",__query.query),ToEscapedStrings("page",__query.page),ToEscapedStrings("opt",__query.opt)]));}}
Now, the URL builder for CounterWithQuery can accept queries. For more details on query definitions, refer to Query Support.
The URL builder function is (ClassName).Helper.(VariableName). You can use it as follows:
Usage.cs
1 2 3 4 5 6 7 8 9101112131415161718
// Call the function to generate a URLvarhomeUrl=WebPaths.Helper.Index();// With parametersvarcounterStateUrl=WebPaths.Helper.CounterWithState(1);// With queriesvarcounterQueryUrl1=WebPaths.Helper.CounterWithQuery(new());varcounterQueryUrl2=WebPaths.Helper.CounterWithQuery(new(){query="test"});varcounterQueryUrl3=WebPaths.Helper.CounterWithQuery(new(){query="foo",page=1,opt=true});Console.WriteLine(homeUrl);// -> "/"Console.WriteLine(counterStateUrl);// -> "/counter/1"Console.WriteLine(counterQueryUrl1);// -> "/counter/query?query=hello&page=0"Console.WriteLine(counterQueryUrl2);// -> "/counter/query?query=test&page=0"Console.WriteLine(counterQueryUrl3);// -> "/counter/query?query=foo&page=1&opt=true"// To navigate to another page// @inject NavigationManager NavNav.NavigateTo(counterStateUrl);
On the Blazor page side, you need to specify attributes like @page, [Parameter], and [SupplyParameterFromQuery], but these can also be automatically generated based on URL definition information.
usingBlazorPathHelper;usingMicrosoft.AspNetCore.Components;[BlazorPath]publicpartialclassWebPaths{[Page<Home>]// <- Add the Page<PageComponent> attributepublicconststringIndex="/";[Page<Sample>]publicconststringSample="/sample";[Page<SampleChild>]publicconststringSampleChild="/sample/child";[Page<Counter>]publicconststringCounter="/counter";[Page<Counter2>]publicconststringCounterWithState="/counter/{count:int}";[Page<Counter3>, Query<QueryRecord>]publicconststringCounterWithQuery="/counter/query";}publicrecordQueryRecord(stringquery="hello",intpage=0,bool?opt=null);// Definitions for each component (actually written in each component)publicpartialclassHome:ComponentBase;publicpartialclassSample:ComponentBase;publicpartialclassSampleChild:ComponentBase;publicpartialclassCounter:ComponentBase;publicpartialclassCounter2:ComponentBase;publicpartialclassCounter3:ComponentBase;
This will automatically generate files that include class definitions with @page, [Parameter], and [SupplyParameterFromQuery].
// <auto-generated />// -------------------// Index(Home)[Route("/")]// Equivalent to writing [@page "/"] in a razor filepublicpartialclassHome;// Omitted for brevity// CounterWithState(Counter2)[Route("/counter/{count:int}")]publicpartialclassCounter2{// Parameter definitions are automatically generated[Parameter]publicintCount{get;set;}}// -------------------// CounterWithQuery(Counter3)[Route("/counter/query")]publicpartialclassCounter3{// Query parameter definitions are automatically generated[SupplyParameterFromQuery]publicstringQuery{get;set;}[SupplyParameterFromQuery]publicintPage{get;set;}[SupplyParameterFromQuery]publicbool?Opt{get;set;}}
With this, page attributes are automatically set, eliminating the need to write them in .razor files. You can remove the @page attribute and others from .razor files. For detailed usage, refer to Automatic Page Attributes.
You Can Remove It Anytime
The automatic generation by the [Page] attribute is a completely independent feature. Therefore, if you don't need the automatic definitions, you can remove the [Page] attribute at any time.
usingBlazorPathHelper;[BlazorPath]publicpartialclassWebPaths{// The [Item("MenuName")] attribute enables automatic generation.// It is completely independent of the [Page]/[Query] attributes, so they can be used together.[Item("TopPage")]publicconststringIndex="/";[Item("Sample1a")]publicconststringSample="/sample";[Item("Sample1b")]publicconststringSampleChild="/sample/child";[Item("Sample2a")]publicconststringCounter="/counter";// Pages with parameters/queries should not be displayed in the menu, so the [Item] attribute is omitted.// (Even if set, they are automatically omitted from the menu)publicconststringCounterWithState="/counter/{count:int}";}
This will automatically generate menu structure data in WebPaths.MenuItem.
Automatically Generated Menu Structure Data (Collapsible)
// <auto-generated />publicpartialclassWebPaths{publicstaticreadonlyBlazorPathMenuItem[]MenuItem=[newBlazorPathMenuItem(){Name="TopPage",// Menu name. If not specified, the variable name is usedPath="/",// Page URLChildren=[]// Submenu// There are several other properties that are automatically generated to help with menu creation.},newBlazorPathMenuItem(){Name="Sample1a",Path="/sample",// Submenus are automatically generated based on the URL structureChildren=[newBlazorPathMenuItem(){Name="Sample1b",Path="/sample/child",Children=[]}]},newBlazorPathMenuItem(){Name="Sample2a",Path="/counter",Children=[]}]}
Here, only the menu structure data is generated, and you need to implement the menu display separately. It's up to you which framework you use to create the menu!
For details, refer to the samples for each framework.