Query Support
Basic Usage¶
The minimum requirements are as follows:
- Prepare a class with the
[BlazorPath]
attribute. This class must be defined with thepartial
keyword. - Define constants of type
const string
as members within this class. - Add the
[Query<QueryClass>]
attribute to these members.
During the process of generating URL builder functions, a query-compatible version of the function will be created based on the [Query<QueryClass>]
definition.
WebPaths.cs | |
---|---|
1 2 3 4 5 6 7 8 |
|
Caution!
The QueryRecord
class must be written in a .cs
file. Due to the specifications of the SourceGenerator, it cannot be written in a .razor
file.
Recommendation: Clearly define default parameter values
It is recommended to specify default values for each parameter or make them nullable. (You need to consider the case where query parameters are not specified.)
Generated Code
Auto Generated Code | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
How It Works¶
For members with the [Query<QueryClass>]
attribute, the following function is generated:
Auto Generated Code | |
---|---|
1 2 3 4 5 6 |
|
As you can see from the generated code above, each property of the QueryRecord
class is expanded as a query parameter. When generating this function, the BlazorPathHelper performs the following internally:
- Checks if the
[Page<QueryClass>]
attribute is present. - If specified, extracts the properties defined in
QueryClass
.- Members can also be used, but it is not recommended.
- Calls the
ToEscapedStrings
function for each property to generate query parameters.- For example, in the above case, strings like
"query=hello"
,"page=0"
,"opt=true"
are generated.
- For example, in the above case, strings like
- Passes the generated query parameters to the
BuildQuery
function to create the query string.- In the above case, a string like
"?query=hello&page=0&opt=true"
is generated. - If
opt=null
, it becomes"?query=hello&page=0"
, and theopt
parameter is not output.
- In the above case, a string like
Nested properties are not supported
As defined above, the class itself is not restored; instead, properties are extracted once. Therefore, nested properties are not currently supported.
Changing Query Names¶
When you specify the [Query<QueryRecord>]
attribute, the query name uses the property name of QueryRecord
as is. If you want to change the query name, add the [QueryName("shortName")]
attribute.
WebPaths.cs | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Supported Types¶
The following definitions are possible for properties of QueryClass
.
Standard Types¶
Example Class Definition | Example Output Query URL |
---|---|
record QueryClass(int val1) |
"/?val1=5" |
record QueryClass(bool? flg1) |
"/?flg1=true", "/" |
In addition to these, any type that implements ToString()
and can be restored by Blazor should generally be supported.
Arrays¶
Example Class Definition | Example Output Query URL |
---|---|
record QueryClass(string[] arr) |
"/?arr=foo&arr=bar&arr=buz" |
In addition to string[]
, types like int[]
and bool[]
are also supported. However, IEnumerable
and List
are not supported as Blazor does not handle them.