Index
最小使用方法
以下是最低限必要的内容:
- 准备一个带有
[BlazorPath]
属性的类。在类定义中需要使用 partial
属性。
- 在该类中定义一个
const string
类型的常量作为成员。
BlazorPathHelper 会自动扫描满足上述条件的类定义,并生成 URL 构建函数。
WebPaths.cs |
---|
| [BlazorPath]
public partial class WebPaths
{
public const string CounterWithState = "/counter/{count:int}";
}
|
生成的代码
自动生成的代码 |
---|
| // <auto-generated />
public partial class WebPaths
{
public partial class Helper
{
public static string CounterWithState(int Count)
=> string.Format("/counter/{0}", ToStringForUrl(Count));
}
}
|
URL定义示例
官方文档中列出的所有URL定义都支持。请参阅该程序的测试用例。
常规示例
定义示例 |
生成的函数 |
输出的URL示例 |
"/{text}" |
string Text(string text) |
"/sample-text" |
"/{active:bool}" |
string Active(bool active) |
"/true" |
"/{dob:datetime}" |
string Dob(DateTime dob) |
"/2025-01-01T00:00:00Z" ※1 |
"/{price:decimal}" |
string Price(decimal price) |
"/123.45" |
"/{weight:double}" |
string Weight(double weight) |
"/123.45" |
"/{weight:float}" |
string Weight(float weight) |
"/123.45" |
"/{id:guid}" |
string Id(Guid id) |
"/00001111-aaaa-2222-bbbb-3333cccc4444" |
"/{id:int}" |
string Id(int id) |
"/123456789" |
"/{ticks:long}" |
string Ticks(long ticks) |
"/123456789" |
"/{parameter:nonfile}" |
string Parameter(string parameter) |
"/foo" |
"/{*pageRoute}" |
string PageRoute(string pageRoute) |
"/foo/bar/buz" |
※1: 输出为ISO 8601格式且为UTC格式。
可选参数示例
定义示例 |
生成的函数 |
输出的URL示例 |
"/{text?}" |
string Text(string? text = null) |
"/sample-text-2", "/" |
"/{active:bool?}" |
string Active(bool? active = null) |
"/true", "/" |
组合示例
定义示例 |
生成的函数 |
输出的URL示例 |
"/{text}/{val:int}" |
string TextValue(string text, int val) |
"/sample/123" |
"/{id:int}/{flag:bool?}" |
string IdFlag(int id, bool? flag = null) |
"/123/true", "/456/" |
各种定义方法
WebPaths.cs |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | using BlazorPathHelper;
[BlazorPath]
public partial class WebPaths
{
// string
public const string WithString = "/string/{val}";
// nullable string
public const string WithStringNullable = "/string-null/{val?}";
// bool
public const string WithBool = "/bool/{val:bool}";
// datetime
public const string WithDate = "/datetime/{val:datetime}";
// decimal
public const string WithDecimal = "/decimal/{val:decimal}";
// double
public const string WithDouble = "/double/{val:double}";
// float
public const string WithFloat = "/float/{val:float}";
// guid
public const string WithGuid = "/guid/{val:guid}";
// int
public const string WithInt = "/int/{val:int}";
// long
public const string WithLong = "/long/{val:long}";
// multiple pattern
public const string WithMultiple = "/multi/{val1:int}/{val2:int}";
// super multiple pattern
public const string WithSuperMultiple = "/something/{val1:string}/{val2:int}/{val3:double?}";
// catch all pattern
public const string WithCatchAll = "/catch-all/{*rest}";
}
|