rabejens
rabejens

Reputation: 8122

Blazor: Route with parameter is not resolved

I have this very simple page:

@page "/somepage/{id}"

@namespace My.BlazorApp.Pages

<h1>This would be page @Id</h1>

@code {

    [Parameter]
    public long Id { get; set; }
}

I have the following code in my NavMenu.razor:

@foreach(var sp in ViewModel.SubPageLinks) {
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="@sp.Path">
            <span class="oi oi-plus" aria-hidden="true"></span> @sp.Title
        </NavLink>
    </li>
}

@sp.Path generates somepage/0, somepage/1 etc.

But when I execute the application, I get "Sorry, there is nothing at this address".

What am I missing?

Upvotes: 0

Views: 1170

Answers (2)

rabejens
rabejens

Reputation: 8122

I found the solution. It had to do with the .csproj file.

Searching on the net revealed this issue on Github, so I made a very simple example to reproduce it, to have that issue reopened. I just created the basic example Blazor app and added a very simple page which just displays "Foo".

When I ran that, I, too, got "Sory, there's nothing at this address".

Then I went into the .csproj and I found the following:

  <ItemGroup>
    <Content Remove="Pages\SomeOtherPage.razor" />
  </ItemGroup>

  <ItemGroup>
    <None Include="Pages\SomeOtherPage.razor" />
  </ItemGroup>

When I removed this, it started to work. Plus, as @inktkiller states, I forgot to add the :long type definition which first caused an exception to be thrown when opening the page.

The explanation is: I am creating my Razor pages as Text files and rename them afterwards, because when I am using the Razor page wizard, my Visual Studio still generates the old cshtml files. And text files get added to the csproj in that strange manner.

Upvotes: 1

tbdrz
tbdrz

Reputation: 2190

You have to specify :long in your route parameter.

@page "/somepage/{id:long}"

https://learn.microsoft.com/en-us/aspnet/core/blazor/routing?view=aspnetcore-3.1#route-constraints

Upvotes: 2

Related Questions