epj.RouteGenerator 1.0.0-pre1

This is a prerelease version of epj.RouteGenerator.
There is a newer version of this package available.
See the version list below for details.
dotnet add package epj.RouteGenerator --version 1.0.0-pre1
NuGet\Install-Package epj.RouteGenerator -Version 1.0.0-pre1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="epj.RouteGenerator" Version="1.0.0-pre1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add epj.RouteGenerator --version 1.0.0-pre1
#r "nuget: epj.RouteGenerator, 1.0.0-pre1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install epj.RouteGenerator as a Cake Addin
#addin nuget:?package=epj.RouteGenerator&version=1.0.0-pre1&prerelease

// Install epj.RouteGenerator as a Cake Tool
#tool nuget:?package=epj.RouteGenerator&version=1.0.0-pre1&prerelease

epj.RouteGenerator

Tired of manually specifying route identifiers and fixing typos in the route-based navigation of your .NET app? This source generator will take away that pain.

Introduction

Route Generator is a C# source generator that generates a static Routes class for your .NET app containing all route identifiers for your string-based route navigation.

Although the sample project is using .NET MAUI, this generator can also be used with other .NET technologies since it targets .NET Standard 2.0.

Basic Usage

First, add the epj.RouteGenerator nuget package to your target project that contains the classes (i.e. pages) from which routes should be automatically generated.

Then, use the [AutoRoutes] attribute from the epj.RouteGeneratornamespace on one of the classes at the root of your application. This must be within the same project and namespace containing the pages.

You must provide a suffix argument which represents the naming convention for your routes to the attribute, e.g. "Page" like above. It doesn't have to be "Page", it depends on the naming convention you use for pages in your app. If all your page classes end in "Site", then you would pass "Site" to the attribute. This suffix is used in order to identify all classes that should be included as routes in the generated Routes class based on their class name.

When using .NET MAUI, I recommend to use the attribute to decorate the MauiProgram class:

[AutoRoutes("Page")]
public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();

        // ...

        return builder.Build();
    }
}

The source generator will then pick up all pages that end in the specified suffix and generate the Routes class with these identifiers within the same root namespace as the entry point:

// <auto-generated/>
using System.Collections.ObjectModel;

namespace RouteGeneratorSample
{
    public static class Routes
    {
        public const string MainPage = "MainPage";
        public const string VolvoPage = "VolvoPage";
        public const string AudiPage = "AudiPage";
    
        private static List<string> allRoutes = new()
        {
            MainPage,
            VolvoPage,
            AudiPage,
        };
        
        public static ReadOnlyCollection<string> AllRoutes => allRoutes.AsReadOnly();
    }
}

Now, you can use these identifiers for your navigation without having to worry about typos in your string-based route navigation:

await Shell.Current.GoToAsync($"/{Routes.AudiPage}");

If the AudiPage would ever get renamed to some other class name, you would instantly notice, because the compiler wouldn't find the Routes.AudiPage symbol anymore and emit an error, letting you know that it has changed. When using verbatim strings instead of an identifier like this, you would notice this change until the app either crashes or stops behaving the way it should.

Extra Routes

There may be situations where you need to be able to specify extra routes, e.g. when a route doesn't follow the specified naming convention using the suffix or when a routes is defined in a different way (in MAUI or Xamarin.Forms this could be using a <ShellContent> element in XAML).

For situations like these, the Route Generator exposes a second attribute called [ExtraRoute] and it takes a single argument representing the name of the route. You may not pass null, empty strings or whitespace as well as special characters. Duplicates will be ignored.

namespace RouteGeneratorSample;

[AutoRoutes("Page")]
[ExtraRoute("SomeOtherRoute")] // valid
[ExtraRoute("SomeFaulty!Route")] // invalid
[ExtraRoute("YetAnotherRoute")] // valid
[ExtraRoute("YetAnotherRoute")] // ignored, because it's a duplicate
public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        
        // ...

        return builder.Build();
    }
}

This would then result in the following Routes:

// <auto-generated/>
using System.Collections.ObjectModel;

namespace RouteGeneratorSample
{
    public static class Routes
    {
        public const string MainPage = "MainPage";
        public const string VolvoPage = "VolvoPage";
        public const string AudiPage = "AudiPage";
        public const string SomeOtherRoute = "SomeOtherRoute";
        public const string YetAnotherRoute = "YetAnotherRoute";
    
        private static List<string> allRoutes = new()
        {
            MainPage,
            VolvoPage,
            AudiPage,
            SomeOtherRoute,
            YetAnotherRoute
        };
        
        public static ReadOnlyCollection<string> AllRoutes => allRoutes.AsReadOnly();
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on epj.RouteGenerator:

Repository Stars
ewerspej/maui-samples
Sample repository for various .NET MAUI, C# and MVVM features covered in blog
Version Downloads Last updated
1.0.1 1,491 2/26/2024
1.0.1-alpha2 373 2/16/2024
1.0.1-alpha 94 2/14/2024
1.0.0 774 1/22/2024
1.0.0-pre2 217 1/13/2024
1.0.0-pre1 326 12/14/2023