Skip to content

Instantly share code, notes, and snippets.

@iambacon
Last active October 10, 2023 09:02
Show Gist options
  • Save iambacon/648aa62f0c275e20d61585d7aa26d81e to your computer and use it in GitHub Desktop.
Save iambacon/648aa62f0c275e20d61585d7aa26d81e to your computer and use it in GitHub Desktop.
<CascadingAuthenticationState>
<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)">
<Authorizing>
</Authorizing>
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="typeof(MainLayout)">
<h1>Page not found</h1>
<p>Sorry, but there's nothing here!</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
using Microsoft.AspNetCore.Components;
namespace RenewPlus.Web.Shared.Components.Shared
{
public class RedirectToLogin : ComponentBase
{
[Inject]
protected NavigationManager NavigationManager { get; set; }
protected override void OnInitialized()
{
NavigationManager.NavigateTo("login");
}
}
}
@kelvinzap
Copy link

Thank you! I needed this

@ktutnik
Copy link

ktutnik commented Oct 10, 2023

If you got Navigation error on NavigationManager.NavigateTo

you can use OnAfterRender instead of OnInitialized

See [https://stackoverflow.com/a/58077203](this StackOverflow answer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment