dsuryd/dotNetify

Catchall RouteTemplate (*) can't be used in this.Redirect

verystack opened this issue · 5 comments

I have a login page which doubles as my index page and my 404 page for unauthenticated users. Using the catchall "*", the page is no longer routable from server-side this.redirect, even if a second RouteTemplate is created with a different URL pattern. This means page routing using RouteLink doesn't work if the pages route to the index. You have to force a refresh.

Creating a Custom 404 "*" to handle it also doesn't seem to work. It was my idea to avoid the issue by using a custom 404 VM, and having the 404 VM do this.redirect to the index in its constructor, but that doesn't work either.

Possible to address these issues/enhancements in an update?

I'm not entirely clear on the issue. Is it something that's broken in latest release, or is it an enhancement request? Can you show me what your route templates code look like?

My route template on my Layout VM looks similar to this. It'd be fantastic to either be able to specify this.redirect("", "*") on other pages, or be able to create a secondary RouteTemplate with the same VM to assign a path to. As far as I have tested neither of these is possible right now, though there could be a workaround I'm not familiar with.

public LayoutVM()
        {
            this.RegisterRoutes("/", new List<RouteTemplate>
            {
                new RouteTemplate(nameof(Route.LoginVM)) { UrlPattern = "*"},
            }
        }

What I feel might be a bug (though not sure if it has ever worked, I can't remember using it) is this.Redirect not working inside a VM constructor with this.OnRouted.
i.e.

public External404VM()
        {
            this.OnRouted((sender, e) =>
            {
                this.Redirect("", "login");
            });

        }

Want to do a HTTP 300-style redirect and don't know how else you'd do it. Considering that this.Redirect is a server-side API, I would have thought that this behaviour would be possible without needing to trigger it through user action on the client side.

If you just want simple redirect on an invalid URL, you don't need to register explicit catch-all route. Add wwwroot/404.html with an inline script to redirect to homepage.

Per the redirect, it was more for if the user was trying to access a page that wasn't validated by some kind of parameter or access level.

Say a reset X page, which requires a token in the URL params.
You can verify the token exists in the URL with this.onRouted, but if it doesn't or is invalid, how would you redirect this to a different VM & component?

Take your book store example on the Dotnetify site. What if a book was deleted, but someone tried to access the book detail view for it? How would you re-direct them?

Hi, first of all, than's for bringing up this issue. It's indeed not clear from existing example or documentation how one woud implement redirection, so I've updated the book store example to demonstrate it (b25e7c1).

The main concept is that it's the client that does the redirect, not the server. The server only provides sufficient information for the client to take action, which in this case is the route to redirect to. The server provides the route with this.Redirect(), and the client routes it with vm.$routeTo().

Let me know if it does not completely address this issue.