Blazor客户端项目Pages文件夹添加子文件夹后路由访问异常问题
Hey there, let's get that navigation working for your Time Log page. The problem is a simple mismatch between your NavLink target and the route defined in your Log.razor component—here's how to fix it step by step:
1. Update the Route in Log.razor
Your Log.razor sits in the Pages/TimeLogs subfolder, so aligning the component's route with this structure keeps your project organized and intuitive. Change the @page directive from /log to /timelogs/log:
@page "/timelogs/log" @using Nessie.Shared @inject HttpClient Http <h1>put info here</h1> the data can be inserted here @code { }
2. Fix the NavLink Href
Your current NavLink points to TimeLog/log, which doesn't match the route we just set. Update the href value to match the component's new route. Using an absolute path (starting with /) ensures it works no matter where the NavMenu is rendered:
<li class="nav-item px-3"> <NavLink class="nav-link" href="/timelogs/log"> <span class="oi oi-list-rich" aria-hidden="true"></span> Time Log </NavLink> </li>
Quick Explanation
Blazor relies entirely on the @page directive to map URLs to components—the folder structure doesn't automatically define routes. In your original setup, the NavLink tried to navigate to TimeLog/log, but your component was only registered for the /log route. By aligning both values, Blazor can now find and render the correct page when the link is clicked.
内容的提问来源于stack exchange,提问作者Sorush

