Blazor upgrading from .NET 6 to .NET 8 – NavigateTo not working

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật