aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Routing enhancements

Open SteveSandersonMS opened this issue 8 years ago • 31 comments

This issue is to track routing features we're likely to want to add in the future:

  • Review exactly which parameter constraints we really want to support. For example maybe add regex; maybe remove bool or limit the set of datetime formats more precisely. See https://github.com/aspnet/blazor/issues/295 for more discussion.
  • Catch-all segments (e.g., /products/{*categoryAndName})
  • 404 handler (it's probably not enough just to have a component that matches /{*anythingElse}, because for SSR you'd want to know it's really not a match so you could actually return a 404) - Done!
  • Simple method for accessing querystring/hash values (e.g., via new properties on IUriHelper)
  • Multiple nested routers
  • Outbound URL generation (e.g., <a page=@About>About us</a>, or perhaps just by codegenning methods like <a [email protected](53)>Duck boots</a> for each @page declaration)
  • In the @page directive, allow the use of compile-time constant expressions, not just literal strings (e.g., https://github.com/aspnet/blazor/issues/1363).
  • Optional route parameters

Also the items from https://github.com/aspnet/blazor/issues/636 that were not yet finalised:

  • Discover [Route] attributes on types in referenced assemblies that themselves reference Microsoft.AspNetCore.Blazor transitively
  • Discover [Route] attributes on component base classes (i.e., inherit=true)
  • In the Router component, make it simple to subclass and override a virtual method to change the logic for locating the IComponent for a given URL. Currently there's no virtual method for this.
  • Make RouteTable's Routes property get-only
  • Cache route tables per assembly

SteveSandersonMS avatar Mar 19 '18 11:03 SteveSandersonMS

@SteveSandersonMS Thanks for raising this!

kristapsstrals avatar Mar 19 '18 11:03 kristapsstrals

is a routine regex constraint on the road map?

grahamehorner avatar Mar 20 '18 01:03 grahamehorner

Regex constraints: definitely maybe :) We're not in a rush to put in all possible constraints because in practice it's rare that you really need them, so they end up just being friction for developers. It's often only server-side API endpoints that need to be strict about that. It's a matter of seeing what scenarios evolve and what kinds of feature requests we're getting from a wide range of people.

SteveSandersonMS avatar Mar 20 '18 10:03 SteveSandersonMS

Would it be possible to define all the routes and destinations in a single place like in other JS frameworks?

dlr1 avatar Mar 24 '18 10:03 dlr1

@dlr1 I believe the routing engine is pluggable so you can replace with any other system you desire.

conficient avatar Mar 26 '18 08:03 conficient

@dlr1 with the ability to nest routes too, perhaps similar to the way angular works with the router-outlet component.

Nathan-Ryan avatar Apr 12 '18 09:04 Nathan-Ryan

Also saving states for every route would be nice feature to have ;)

TheAifam5 avatar Apr 12 '18 19:04 TheAifam5

Please replace 'Multiple nested routers' with 'Unlimited nested routers' else we get the same dilemma they found out later in angular 2 that the router`s nesting skills were limited to sth. like 3 child routers :P

bastienJS avatar Apr 16 '18 20:04 bastienJS

It would be nice to have some virtual methods that are called at appropriate time to either allow navigation to a page or to dis-allow navigation from a page

dlr1 avatar May 29 '18 23:05 dlr1

@SteveSandersonMS Is it possible to restrict the routing from a certain slug onwards? (e.g. everything below http://myserver/blazor will be processed, but e.g. http://myserver/otherdata won't?)

DNF-SaS avatar Jun 08 '18 10:06 DNF-SaS

@DNF-SaS I believe your looking for support for catch-all segments, which hasn't been implemented yet.

danroth27 avatar Jun 12 '18 06:06 danroth27

Is there anything tracking how routes behave if a user is unauthenticated vs authenticated? In MVC we can use filters on controllers to create secure routes and insecure routes.

conficient avatar Jun 15 '18 17:06 conficient

@conficient Not currently.

danroth27 avatar Jun 15 '18 20:06 danroth27

I'm building a Bing Maps component and it generates lots of anchor tags with href="#". As of version 0.5.1 whenever I click any of those links i get redirected to /. Is there a way to proper handle this?

rpedretti avatar Aug 20 '18 15:08 rpedretti

@rpedretti for now you don't have a choice but to use JS interop since stopping the click even to propagate isn't possible yet and is tracked here #1073. If I remember you'd need the event that called the click and prevent it propagating via event.stopPropagation() method.

RyoukoKonpaku avatar Aug 20 '18 16:08 RyoukoKonpaku

Thanki you @RyoukoKonpaku! Worked like a charm!

rpedretti avatar Aug 20 '18 19:08 rpedretti

Is there any way to manually implement 404 handling at the moment? Is this just a case of changing some logic in the routing system or would it require some more profound changes? It would be very useful for a project that I am working on.

TheFanatr avatar Oct 07 '18 00:10 TheFanatr

After combing through some of Blazor's codebase, I think I have a reasonable idea of what needs to be done; would a pull request for something like this be considered, or is it too small? It needs to be made either way for my purposes, but I'd love to submit it to the project if I actually get it working.

TheFanatr avatar Oct 07 '18 01:10 TheFanatr

I got this to work.

<Router AppAssembly=typeof(Program).Assembly FallbackRoute="/404"/>

Basically, if the router detects that a certain route doesn't exist, it will try to render the route described by FallbackRoute and if FallbackRoute is null or nonexistent, the regular behaviour will be observed, with the only difference being that the exception text will say that it cannot find any component with the fallback route '/<fallback>'.

Also, I specifically made it so that URI displayed by the browser is still what was navigated to originally so that some interop can be done to have behaviour specific to a certain nonexistent URI, such as redirecting if a page moved.

I will make a pull request and post the link here.

TheFanatr avatar Oct 07 '18 19:10 TheFanatr

Will there be some way of configuring the router in Startup?

For instance, I want to be able to add handlers that hook into the router and determine whether a component actually should render, like in the case of authentication.

marinasundstrom avatar Oct 19 '18 21:10 marinasundstrom