aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Better Router

Open MihaMarkic opened this issue 8 years ago • 4 comments

Right now Router routes based on @page statement on component. There are at least few disadvantages to this. a) Each page has a single route (i.e. localization, same component on two or more addresses?) b) Routes aren't in one place - not easy to have an overview c) Can't pass parameters to pages

Perhaps something fluent like:

new Router()
  .WithPage<FirstPage>("/firstPage").WithArgument(nameof(FirstPage.OneArgument), 1)
  .WithPage<SecondPage>("/secondPage").WithArgument(nameof(SecondPage.TwoArgument), "two");

or better

new Router()
  .WithPage<FirstPage>("/firstPage", () => new FirstPage { OneArgument = 1 })
  .WithPage<SecondPage>("/secondPage", new SecondPage { TwoArgument) ="two" });

or simply in razor

<router>
    <route src="/firstPage"><FirstPage OneArgument=1 /></route>
    <route src="/secondPage"><FirstPage TwoArgument="two" /></route>
</router>

This examples just brainstorming.

MihaMarkic avatar Mar 26 '18 18:03 MihaMarkic

I was asking same question few days back. https://github.com/aspnet/Blazor/issues/293 Along the same lines you mentioned,

  1. the component doesn't need to be routed at all but the component is part of many pages with routes (read on Gitter, even if the @page statement is not there it routes to that page anyway if the name is in the url)
  2. It will be even better, if there are named areas where we can direct components to.
  3. optional parameters

dlr1 avatar Mar 26 '18 18:03 dlr1

At least (a) and (c) are not completely correct. A page can have multiple routes and parameters (URL and query) are possible (read more). However, I also think that having all routes in a central place would be convenient.

rstropek avatar Mar 26 '18 18:03 rstropek

Ah, didn't know about that documentation. Thanks @rstropek. However, parameters in URL are probably too simple. Imagine a redux scenario where you pass states around. So I'm looking to pass class instances around as well.

Plus routes expressed with @route might be hard to translate (I imagine having a method named TranslateRoute where I would do the translation between diffierent languages).

MihaMarkic avatar Mar 26 '18 19:03 MihaMarkic

If you want to implement completely custom routing logic, you'll be able to subclass Router and override its GetComponentForRoute virtual method (once aspnet/Blazor#266 is finished). You can then write arbitrary logic to get your route config from a central place, or localise routes, or whatever else.

We do still have other routing enhancements planned and tracked via aspnet/AspNetCore#5489, so I'll close this in favour of that one.

SteveSandersonMS avatar Mar 29 '18 13:03 SteveSandersonMS