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.