Open CoCoNuTeK opened 1 month ago
Thanks for the report. What is interesting is that HtmxorNavigationException
inherits from NavigationException
, so the behavior should be preserved, at least in my testing.
Can you provide a minimal runnable code sample that I can use to understand your scenario better?
Using your pizza example code, you can place RedirectManager.RedirectTo("/myorders"); inside the OnInitializedAsync method of Index.razor. Redirections don't seem to work with RedirectManager at all. My idea was to use HTMX redirection via HX-Redirect, which performs a client-side redirect to a new location. I'm unsure how to trigger that directly from the @code section, but it would likely provide a smoother experience, especially with Blazor SSR.
In contrast, Blazor's typical way to handle redirection is by throwing a NavigationManagerException, which the framework manages as a redirect. Is there any way to return custom headers from a Blazor component? While Razor pages handle HTML injection well with direct routing, I'm not sure how to modify the headers (e.g., to include an HTTP request header for the HX-Redirect).
Using your pizza example code, you can place RedirectManager.RedirectTo("/myorders"); inside the OnInitializedAsync method of Index.razor. Redirections don't seem to work with RedirectManager at all.
I assume you are talking about the IdentityRedirectManager
type? Looks like you are not the only one that experience this (https://github.com/dotnet/aspnetcore/issues/53996).
Anyway, Htmxor do the same thing as Blazor SSR, but it provides its own HtmxorNavigationManager
(registrered by default) that throws HtmxorNavigationException
that has additional data included which allows Hrmxor to redirect in the expected way.
So, the behavior should be identical to Blazor SSR, but do check if your code is using HtmxorNavigationManager
.
The key part of the HtmxorRenderer that handles this is here:
I'm unsure how to trigger that directly from the @code section, but it would likely provide a smoother experience, especially with Blazor SSR.
You need access to the HtmxContext
object that is created with each request, and is available as an injected service as well as a cascading value, Then, e.g. during OnInitialized, you can set HX response headers that will cause htmx to do client side stuff, e.g.:
// note: this is untested code
[CascadingParameter]
public HtmxContext Context { get; set; }
protected override void OnInitialized()
{
Context.Response.Location("/url/to/other/page");
}
Btw. I have been away from this project for a few months, simply have not had the time, so some of this is based on vague memories and may not all be correct.
Whenever I attempt to call
RedirectManager.RedirectTo
, I expectNavigationManager.NavigateTo(uri)
to throw aNavigationException
, which should be handled by the framework to facilitate the redirection to the specified URL.However, instead of triggering the expected
NavigationException
, what gets triggered is anHtmxorNavigationException
. This leads to anObject reference not set to an instance of an object
error, which ultimately prevents the redirection from working properly when the Htmxor package is active.Expected Behavior:
NavigationManager.NavigateTo(uri)
should trigger theNavigationException
, allowing the framework to handle the redirection seamlessly.Actual Behavior:
HtmxorNavigationException
is thrown instead, causing a null reference error and breaking the redirection flow.Steps to Reproduce:
RedirectManager.RedirectTo
with Htmxor active.NavigationException
is not triggered, and redirection fails due toHtmxorNavigationException
.