Skip to content

URL Builder

Basic Usage

The minimum requirements are as follows:

  • Prepare a class with the [BlazorPath] attribute. This class definition must also include the partial attribute.
  • Define constants of type const string as members within that class.

BlazorPathHelper will automatically search for class definitions that meet the above criteria and generate URL builder functions.

WebPaths.cs
1
2
3
4
5
[BlazorPath]
public partial class WebPaths
{
  public const string CounterWithState = "/counter/{count:int}";
}
Generated Code
Auto Generated Code
1
2
3
4
5
6
7
8
9
// <auto-generated />
public partial class WebPaths
{
  public partial class Helper
  {
    public static string CounterWithState(int Count)
      => string.Format("/counter/{0}", ToStringForUrl(Count));
  }
}

URL Definition Examples

All URL definitions listed in the official documentation are supported. You can also refer to the test cases for this program.

Standard Examples

Definition Example Generated Function Example Output 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: The output is converted to ISO 8601 format and UTC format.

Optional Parameter Examples

Definition Example Generated Function Example Output URL
"/{text?}" string Text(string? text = null) "/sample-text-2", "/"
"/{active:bool?}" string Active(bool? active = null) "/true", "/"

Combined Examples

Definition Example Generated Function Example Output 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/"

Various Definition Methods

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}";
}