Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor: A more flexible and easy-to-use router. #11671

Closed
hez2010 opened this issue Jun 28, 2019 · 3 comments
Closed

Blazor: A more flexible and easy-to-use router. #11671

hez2010 opened this issue Jun 28, 2019 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@hez2010
Copy link
Contributor

hez2010 commented Jun 28, 2019

Is your feature request related to a problem? Please describe.

Feature request.
Currently we can only pass parameters via query in Router, and also we cannot apply Layout to NotFoundContent. It's not flexible at all and it's really hard to use.

Describe the solution you'd like

I'd like to introduce a <Switch> which performs like a <Frame> in UWP, and we can use <Route> to set routed content and path, also we can pass parameters from both query and component parameters, and query parameters are automatically included.
And it matches routes by the order from the top to bottom.

<Switch>
    <Route> <!-- multiple templates -->
        <Route.path Path="template1"></Route.path>
        <Route.path Path="template2"></Route.path>
        <MyComponent1 Para1="..."></MyComponent1>
    </Route>
    <Route path="template3"> <!-- single template -->
        <MyComponent2 Para1="..." Para2="..."></MyComponent2>
    </Route>
    <Route> <!-- if a component set @page, we can ignore the path property of Route -->
        <MyComponent3 Para1="..."></MyComponent3>
    </Route>
    <Route> <!-- Not Found Route -->
        <MyNotFoundComponent Para1="..." Para2="..." Para3="..."></MyNotFoundComponent>
    </Route>
</Switch>

It performs like:

RenderFragment content = Url switch 
{
    var url when url.IsMatch("template1") || url.IsMatch("template2") => MyComponent1,
    var url when url.IsMatch("template3") => MyComponent2,
   _ => MyNotFoundComponent,
};

In this way, we can also put <Switch> in our own custom layout and apply layout to NotFoundContent.
With this, we can do more things. For example, we can store UserInfo in the Layout and define a function to fetch FetchUserInfo in the Layout, and we can pass the UserInfo and FetchUserInfo to some routed components, in the end we can display different contents based on the state of UserInfo and invokeFetchUserInfo() when necessary.

If we want to use automatically routing, we can write:

<Switch Assembly="typeof(Program).Assembly">
    <Route> <!-- Not Found Route -->
        <MyNotFoundComponent Para1="..." Para2="..."></MyNotFoundComponent>
    </Route>
</Switch>

Which appends the assemblies above the "Not Found Route", however, with this form we can only pass parameters from query to our routed components.

Additional context

It will solve #10445. And in my option it's a better solution comparing to #10493.

Or, if Blazor has a mechanism for state management (#5467), the above changes will be less necessary.

@Eilon Eilon added the area-blazor Includes: Blazor, Razor Components label Jun 28, 2019
@SteveSandersonMS
Copy link
Member

It's an interesting idea, @hez2010!

I'd recommend you implement this and see how well you feel it works in practice. As far as I'm aware, there's nothing to stop some <Switch> component like this from being implemented in application code (it doesn't have to be done in the framework repo).

If you find it's much better, you might want to publish it as a NuGet package so others can use it. In the long run if it becomes established that this is a better system, we might consider including it in the framework by default.

@SteveSandersonMS
Copy link
Member

Closing as it's not something we plan to take action on in this repo in the immediate future, but marking as discussion. Please feel free to continue discussing here if you wish.

@hez2010
Copy link
Contributor Author

hez2010 commented Jun 28, 2019

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

3 participants