Can't navigate from Blazor Client to Blazor Server page

Bernhard S 126 Reputation points
2023-01-17T20:41:13.3133333+00:00

Testing a Blazor Webassembly hosted app template I try to navigate from NavMenu.razor to a page from the server. The reasons:

Blazor.Client

NavMenuRazor.cs

<div class="@NavMenuCssClass nav-scrollable" @onclick="ToggleNavMenu">
    <nav class="flex-column">
        <div class="nav-item px-3">
            <NavLink class="nav-link" href="authorizetest">
                <span class="oi oi-list-rich" aria-hidden="true"></span> Authorize
            </NavLink>
        </div>
    </nav>
</div>

Blazor.Server

AuthorizeTest.razor

@page "/authorizetest"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]

<h3>Authorized!</h3>
<p>Congratulations! You are allowed to see this!</p>


@code {

}

Error

[https://localhost:7138/authorizetest

Sorry, there's nothing at this address.

Question

Is this possible, and if, how?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,652 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,846 Reputation points Microsoft Vendor
    2023-01-18T02:56:33.98+00:00

    Hi @Bernhard S

    You should add the Razor Page/Components in the Client project, instead of the Server project.

    A hosted Blazor WebAssembly solution includes the following ASP.NET Core projects:

    • "Client": The Blazor WebAssembly app.
    • "Server": An app that serves the Blazor WebAssembly app and weather data to clients.
    • "Shared": A project that maintains common classes, methods, and resources. You can check the Server project Program.cs file, it doesn't configure the Blazor routing for the Razor components, so the navigate not working.

    For the server project authorization issue, you can also configure JWT Authentication on the server project.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.