Search code examples
c#asp.netblazor

Blazor not discovering new pages in subfolders


I create a new page, e.g. in Pages/Clients/ subfolder with following code:

@page "/clients"
@inject NavigationManager NavigationManager
<div>My clients page</div>

I put the link:

<NavLink href="/clients" class="nav-link">Clients</NavLink>

When I click on link the page shows me default message

Sorry, there's nothing at this address.

Then I stop the app. Put my clients page in "Pages" folder. Run the application and I see the proper page with

My clients page

Ok, then I move my page file again to Pages/Clients/ subfolder, run the app and... it still works! Moreover - If I do any changes to Clients.razor page it is being shown correctly in browser.

So it looks like Blazor cannot find the page in subfolder for the first time, but after it "registers" it somehow it could find and show it correctly all the time.

I have the

<base href="~/" />

line in my _Host.cshtml file


Solution

  • Well, I was fighting with it for a couple of days and right after posting on Stackoverflow I accidentally found an answer. In your .csproj file there were following lines:

      <ItemGroup>
        <Watch Include="**\*.razor" />
      </ItemGroup>
    
      <ItemGroup>
        <Content Remove="Pages\Activities\ActivitiesList.razor" />
        <Content Remove="Pages\Entrance\Entrance.razor" />
        <Content Remove="Pages\Instructors\InstructorsList.razor" />
        <Content Remove="Pages\Plan\Plan.razor" />
      </ItemGroup>
    
      <ItemGroup>
        <None Include="Pages\Activities\ActivitiesList.razor" />
        <None Include="Pages\Administration\Administration.razor" />
        <None Include="Pages\Entrance\Entrance.razor" />
        <None Include="Pages\Instructors\InstructorsList.razor" />
        <None Include="Pages\Plan\Plan.razor" />
      </ItemGroup>
    
      <ItemGroup>
        <Watch Remove="Pages\Activities\ActivitiesList.razor" />
        <Watch Remove="Pages\Administration\Administration.razor" />
        <Watch Remove="Pages\Entrance\Entrance.razor" />
        <Watch Remove="Pages\Instructors\InstructorsList.razor" />
        <Watch Remove="Pages\Plan\Plan.razor" />
      </ItemGroup>
    

    I removed most of them and left only this:

      <ItemGroup>
        <Watch Include="**\*.razor" />
      </ItemGroup>
    

    All the files are being now discovered correctly. Just remember to check your csproj if some garbage aren't being generated there during development process.