Skip to content

Automatic Page Attributes

Basic Usage

Here's the minimum you need to do:

  • Create a class with the [BlazorPath] attribute. Make sure the class definition includes the partial keyword.
  • Define a constant of type const string as a member within that class.
  • Add the [Page<PageComponent>] attribute to the member.

BlazorPathHelper will automatically scan the class definitions that meet these criteria and generate URL builder functions.

WebPaths.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
using BlazorPathHelper;
using Microsoft.AspNetCore.Components;

[BlazorPath]
public partial class WebPaths
{
  [Page<Home>]
  public const string Index = "/";
}

// Definition for each component (typically written within each component)
public partial class Home : ComponentBase;
Generated Code
Auto Generated Code
1
2
3
// <auto-generated />
[Route("/")]
public partial class Home;

As a result, the Home component will automatically have the same effect as if it had an @page attribute.

Note!

You need to remove the originally defined @page attribute at the end.

Parameter and Query Support

If the URL includes parameter definitions, the [Parameter] attribute will be automatically added.

WebPaths.cs
1
2
3
4
5
6
7
8
using BlazorPathHelper;

[BlazorPath]
public partial class WebPaths
{
  [Page<Counter>]
  public const string CounterWithState = "/counter/{count:int}";
}
Generated Code
Auto Generated Code
1
2
3
4
5
6
7
// <auto-generated />
[Route("/counter/{count:int}")]
public partial class Counter
{
  [Parameter]
  public int Count { get; set; }
}

Similarly, by adding the [Query<QueryRecord>] attribute, the [SupplyParameterFromQuery] attribute will be automatically added.

WebPaths.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using BlazorPathHelper;

[BlazorPath]
public partial class WebPaths
{
  [Query<QueryRecord>, Page<Counter2>]
  public const string CounterWithQuery = "/counter/query";
}

public record QueryRecord(string query = "hello", int page = 0, bool? opt = null);
Generated Code
Auto Generated Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// <auto-generated />
[Route("/counter/query")]
public partial class Counter2
{
  [SupplyParameterFromQuery]
  public string Query { get; set; }

  [SupplyParameterFromQuery]
  public int Page { get; set; }

  [SupplyParameterFromQuery]
  public bool? Opt { get; set; }
}

It also supports shortened query names.

WebPaths.cs
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
using BlazorPathHelper;

[BlazorPath]
public partial class WebPaths
{
  [Query<QueryRecord>, Page<Counter3>]
  public const string CounterWithQuery = "/counter/query";
}

public record QueryRecord
{
  [SupplyParameterFromQuery(Name = "q")]
  public string Query { get; init; } = "hello";

  [SupplyParameterFromQuery(Name = "p")]
  public int Page { get; init; } = 0;

  [SupplyParameterFromQuery(Name = "o")]
  public bool? Opt { get; init; }
}
Generated Code
Auto Generated Code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// <auto-generated />
[Route("/counter/query")]
public partial class Counter3
{
  [SupplyParameterFromQuery(Name = "q")]
  public string Query { get; set; }

  [SupplyParameterFromQuery(Name = "p")]
  public int Page { get; set; }

  [SupplyParameterFromQuery(Name = "o")]
  public bool? Opt { get; set; }
}

How It Works

TODO