Skip to content

KuraiAndras/BlazingRoute

Repository files navigation

BlazingRoute Nuget

Source generator for generating strongly-typed methods for Blazor routes.

Code generation

For a project a class is generated with methods to retrieve page routes:

Artists.razor:

@page "/artists/{Id:guid}"

@code {
    [Parameter] public Guid Id { get; set; }
}

Index.razor:

@page "/"

<h1>Home</h1>

Generates the following Routes.cs:

namespace Your.ProjectName
{
    public static partial class Routes
    {
        public static ImmutableArray<string> All { get; } = new []
        {
            "/",
            "/artists/{Id:guid}",
        }.ToImmutableArray();

        /// <summary>
        /// /
        /// </summary>
        public static string Index() => $"/";

        /// <summary>
        /// /
        /// </summary>
        public static void Index(this NavigationManager navigationManager) => navigationManager.NavigateTo($"/");

        /// <summary>
        /// /artists/{Id:guid}
        /// </summary>
        public static string Artists(Guid Id) => $"/artists/{Id.ToString("D", System.Globalization.CultureInfo.InvariantCulture)}";

        /// <summary>
        /// /artists/{Id:guid}
        /// </summary>
        public static string Artists(this NavigationManager navigationManager, Guid Id) => navigationManager.NavigateTo($"/artists/{Id.ToString("D", System.Globalization.CultureInfo.InvariantCulture)}");
    }
}

Usage

@inject NavigationManager Navigation

<NavLink class="nav-link" href="@Routes.Index()">Home Link</NavLink>

<button class="btn btn-primary" @onClick="@Navigate">Some Artist</button>

@code {
    private void Navigate() => Navigation.Artists(Guid.NewGuid());
}

Options

Code generation can be customized. For a project create a file named RouteGeneration.xml in the root of the project. In the project csproj reference that as an Additional file.

  <ItemGroup>
    <AdditionalFiles Include="RouteGeneration.xml" />
  </ItemGroup>
Name Usage Default Value
ClassName Name of the generated class Routes
Namespace Namespace of the generated class Project's assembly name
GenerateExtensions Generate extensions methods for NavigationManager true
ExtensionPrefix Prefix for the extension methods. For example: NavigateTo -> NavigateToIndex Empty string

Sample:

<?xml version="1.0" encoding="UTF-8"?>
<GenerationOptions>
  <ClassName>MyRoutes</ClassName>
  <Namespace>BlazingRoute.Sample.Routes</Namespace>
  <GenerateExtensions>true</GenerateExtensions>
  <ExtensionPrefix>NavigateTo</ExtensionPrefix>
</GenerationOptions>

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published