1

I'm trying to create a github pages website with the new Blazor WASM. I use C# almost every day for work, but I've never used Blazor/Razor/ASP.NET before, so it's very possible that I'm trying to use server-side techniques that won't work here.

I have a group of pages, and I want to show a little preview of each page with a link to that page on the homepage. All of these pages implement the same component base class. For example, Sample.razor might look like

@page "/sample"
@inherits GroupBase

<!-- html here -->
@code {
    public override string Name { get; } = "Sample";
}

Index.razor might look like

@page "/"

@foreach (GroupBase p in mPagesToPreview)
    {
        <a href="   ">@p.Name</a>
    }
@code {
    List<GroupBase> mPagesToPreview = new List<GroupBase> { new Sample() };
}

Is there any way to route to the Sample page without explicitly putting in "/sample"? Or even using a normal link (I've checked both a and NavLink) with a function similar to nameOf() or getType()?

My best option right now is to add a URL property to the base, but it seems like I shouldn't have to type out "/sample" twice. (It's not hard; it's the principle of the thing!)

I've also seen some examples of people injecting a NavigationManager and then using that to deduce the URL, but that would also have to be repeated for each page. I've seen some examples that get the page directive from RouteData.Values, but those were Razor pages. I couldn't figure out how to create a RouteData object or use it as a static class.

LEJ
  • 47
  • 7

1 Answers1

0

You can use class RouteAttribute to find the route. It looks like this:

[Microsoft.AspNetCore.Components.RouteAttribute("/counter")]
public partial class Counter : Microsoft.AspNetCore.Components.ComponentBase

See How do I read an attribute on a class at runtime?

Remi THOMAS
  • 854
  • 6
  • 11