I am moving a functioning .NET 6 application to .NET 8.
This was a wholly WebAssembly project. I have retained that but use the server side rendering of the home page to improve the user experience.
I want to get a user to click an object on a page, log in and then return to that page, where using local storage to remember the object, it sorts it out and displays it etc.
So I need to redirect to the login page with a return url of “/” so not too hard, but it is not working and gets to ‘Not Found’.
The code to do this is set up or called in the ‘OnAfterRender’ method so pretty sure it is local to the browser.
My Routes.Razor
looks like this:
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
My App.razor
has
<HeadOutlet @rendermode="RenderModeForPage" />
with the code as follows:
public partial class App
{
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;
private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Account")
? null
: RenderMode.InteractiveWebAssembly;
}
This ensures that the Account
code stays server side from the MS template code.
I am sure I am missing something stupid re the routing. I tried this too:
string reg = _navigationManager.GetUriWithQueryParameters("Account/Login", new Dictionary<string, object?> { ["ReturnUrl"] = "/" });
_navigationManager.NavigateTo(reg);
… and if I break on the NavigateTo
line, the value for reg is null.
To try to simplify and encapsulate it further I took my login button and changed it (original code commented out):
<div class="ctr">
<div class="clnk">
@* <Button Size="Size.Small" Color="Color.Primary" Type="ButtonType.Link" To="Account/Login">Login/Sign Up</Button> *@
<Button Size="Size.Small" Color="Color.Primary" Clicked="Login">Login/Sign Up</Button>
</div>
</div>
public void Login()
{
_navigationManager.NavigateTo("Account/Login");
}
With the button it works fine as a link, not found when trying to route with NavigateTo
…
What am I missing here? Thanks in advance.
EDIT
The route for the login page is as per the MS template code with an @page directive at the top:
@page "/Account/Login"
2
I am not 100% sure if this is a true answer or work around as yet.
I’ll take it under advisement. However, I found that if I make the call:
_navigationManager.NavigateTo("Account/Login",true);
The ‘true’ bool is the ‘force load’ option which bypasses client side routing.
I am not sure therefore if, depite being called via the onaafterrender, the page is in fact fully home on the client.
I am happy to take further feedback on this issue as the start up of the home page which is initially statically rendered on the server then re-rendered on the client after the WASM has downloaded still holds a few mysteries.