Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add support for multiple routes in a single page#59

Merged
tesar-tech merged 5 commits into
BlazorStatic:masterfrom
junovell:master
Apr 17, 2025
Merged

Add support for multiple routes in a single page#59
tesar-tech merged 5 commits into
BlazorStatic:masterfrom
junovell:master

Conversation

@junovell

Copy link
Copy Markdown
Contributor

Add support for pages that have multiple routes, and some performance improvements.

example:

@page "/blogs"
@page "/ar/blogs"

Previously only the first route (/blogs in this example) will be generated.

This is can be useful for adding translations to a site (i'm working on an example)

@netlify

netlify Bot commented Apr 16, 2025

Copy link
Copy Markdown

Deploy Preview for blazorstatic ready!

Name Link
🔨 Latest commit 0116a63
🔍 Latest deploy log https://app.netlify.com/sites/blazorstatic/deploys/680148cfa5ef4b00082dfd80
😎 Deploy Preview https://deploy-preview-59--blazorstatic.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@tesar-tech tesar-tech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you,

Please add it to some page in the demo website so we can "test".

Also make sure it works without the @page directive. (by specifying the attribute the "classic" way).

Comment thread src/Services/RoutesHelper.cs
Comment thread src/Services/RoutesHelper.cs Outdated
Comment thread src/Services/RoutesHelper.cs
var attr = (RouteAttribute)attributes[i];
if(!attr.Template.Contains('{'))
{
routes[i] = attr.Template;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this will potentially create an array with

  • [0] null
  • [1] something

(right??)

Can we just return only the non null entries? Or change the return type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's actually filtered in the function above

return assembly.ExportedTypes
    .Where(t => t.IsSubclassOf(typeof(ComponentBase)))
    .SelectMany(GetRouteFromComponent)
    .OfType<string>() // only return non null strings

@tesar-tech tesar-tech Apr 17, 2025

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not afraid about that, but about GetRouteFromComponent returning the wrong type.

It should be string?[].

I don't understand why ide (at least rider) isn't yelling at me with its yellow squiggly lines, but it should be that.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, why not something like

component
            .GetCustomAttributes(typeof(RouteAttribute), inherit: false)
            .OfType<RouteAttribute>()
            .Where(attr => !attr.Template.Contains('{'))
            .Select(attr => attr.Template)
            .ToArray();

less lines, clean, always returns string[]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't know, but i didn't have any instance of GetRouteFromComponent returning an array containing a null value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that would happen if you have

@page "/mypage/{myparam}"
@page "/mypage/"

@junovell

Copy link
Copy Markdown
Contributor Author

For the demo should it be a page added to the BlazorStaticWebsite project?
And by "specifying the attribute the 'classic' way" do you mean by using @attribute [Route("...")]?

@tesar-tech

Copy link
Copy Markdown
Collaborator

For the demo should it be a page added to the BlazorStaticWebsite project? And by "specifying the attribute the 'classic' way" do you mean by using @attribute [Route("...")]?

Yes, exactly.

@junovell

Copy link
Copy Markdown
Contributor Author

Done, i added a bullet point in the docs page that links to the demo, I'm not that good at web dev so I just made a basic page showing how it works.

Also whether to use @page or @@attribute [Route(...)] doesn't really matter because of #20 but i included it in the demo either way

@tesar-tech tesar-tech self-requested a review April 17, 2025 18:31
@tesar-tech tesar-tech merged commit 4f78e8a into BlazorStatic:master Apr 17, 2025
@tesar-tech

Copy link
Copy Markdown
Collaborator

Good, thank you. Soon on nuget.

@junovell junovell mentioned this pull request Apr 18, 2025
tesar-tech pushed a commit that referenced this pull request Apr 18, 2025
* make the RoutesHelper class public and rewrite `RoutesHelper.GetRoutesFromComponent`

* minor changes to the MultiRoutes page

* make the multi-route page's UI consistent with other pages

* revert RoutesHelper to be internal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants