I have a Blazor Server app with a page that takes in parameters in the uri. When I click on an anchor tag that has the route setup to hit that page with params (like below) the link works fine, and the page loads.
<a href="/MyPage/{@Param1}/{@Param2}"> <!--link content in here--> </a>
However, if one tries to access the page from that url directly, or manually refreshes in the browser, then the page doesn't reinitialize or hit any breakpoints on the parameters. Instead, it throws a 404 not found.
So two things here:
- Firstly, I'm confused about why it works fine from within the anchor tag, but dies any other way. Especially when pages without params in the
@page
directive work fine with refreshes/direct-urls. - Second, is this an intended behavior for Blazor Server or am I missing something here that's breaking the page refreshes/hitting-url-directly? Doesn't seem like a feature, but maybe I'm misunderstanding Blazor's routing.
Razor and Razor.cs for page in question:
@page "/MyPage/{Param1}/{Param2}"
<h1>MyPage</h1>
<Metrics Param1="@Param1" />
<Details Param1="@Param1" Param2="@Param2" />
<InProgress Param1="@Param1" Param2="@Param2" />
<InQueue Param1="@Param1" />
<br />
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
using MyApp.Data.Models.ApiResponse;
namespace MyApp.Pages
{
public partial class MyPage
{
[Parameter]
public string Param1 { get; set; }
[Parameter]
public string Param2{ get; set; }
public TaskList Tasks { get; set; }
protected override Task OnInitializedAsync()
{
// work in progress, intend to do more here later on
var test = "";
return base.OnInitializedAsync();
}
}
}
Edit(s) -- per comment suggestions
UseEndpoints section of Configure method in Startup.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
After further digging I noticed that the @param2 will occaionally have a .
char in it. Blazor does have a need for configuring routes that have params with dots in them. The below fall back doesn't work:
endpoints.MapFallbackToPage("/MyPage/{Param1}/{Param2}", "/MyPage");
It throws an:
InvalidOperationException: Cannot find the fallback endpoint specified by route values: { page: /MyPage, area: }.
I'm guessing that the area:
being empty is a problem but I'm not finding how or where to properly set that. The example from the link shows just the Page's name in the fallback. Could someone please point out what's wrong with this fall back and how one can properly correct it?
Startup.cs
:endpoints.MapFallbackToFile("index.html");
Start by checking for that. – Selfaggrandizementindex.html
. Also, it might be helpful if you include the entire section starting withapp.UseEndpoints
(where the fallback stuff should be defined) – Selfaggrandizement@param2
occasionally has periods in it. However, the fallback doesn't seem to be working -- will update question in a minute to reflect this. – Aholah