-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Unable to redirect from non-Blazor endpoint to different-origin URL during enhanced navigation #50384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Does Also if you could post a minimal repro that would be very helpful, as it's not clear to me why this shouldn't work by default anyway. |
Thanks for contacting us. <script src="blazor.web.js" autostart="false"></script>
<script>
Blazor.start({ ssr: {disableDomPreservation: true }} );
</script> |
Hi @darena-pjindal. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
Thanks @SteveSandersonMS . The target="_top" worked like magic. Is that the recommended way ? I have uploaded a simple repro here. If you remove the target tag from button on the index page, it stops working. https://github.com/darena-pjindal/BlazorApp6 |
Hi @mkArtakMSFT - It seems to work using the target=_top as suggested by Steve. Do you recommend disabling enhanced navigation |
In general no, it's better to have it on, otherwise you can't preserve interactive components as you navigate around. Are you able to post a minimal repro of the scenario that doesn't work? I just set up a .NET 8 Blazor Web site that contains a non-Blazor rendered page and enhanced nav was able to navigate to it. It would be helpful to understand what's different about your scenario where it doesn't work. Thanks! |
Oh sorry, I just saw your comment above with the repro link. I'll have a look! |
OK, I see this is specific to issuing redirections to external URLs (i.e., on a different origin) from a non-Blazor-rendered endpoint. That's not currently supported by enhanced nav since it would need the server not to return a 301/302, since the client-side code that issued the Possible solutionIf we added the following middleware, that would detect all redirections that occur during enhanced nav requests and turn them into responses that work on the client: app.Use(async (HttpContext ctx, RequestDelegate next) =>
{
await next(ctx);
var redirectionUrl = ctx.Response.Headers.Location.Count > 0 ? ctx.Response.Headers.Location.ToString() : null;
if (!string.IsNullOrEmpty(redirectionUrl) && ctx.Request.Headers.ContainsKey("blazor-enhanced-nav") && IsPossibleExternalDestination(ctx.Request, redirectionUrl))
{
ctx.Response.Headers.Add("blazor-enhanced-nav-redirect-location", redirectionUrl);
ctx.Response.StatusCode = 200;
}
static bool IsPossibleExternalDestination(HttpRequest request, string destinationUrl)
{
if (!Uri.TryCreate(destinationUrl, UriKind.Absolute, out var absoluteUri))
{
return false;
}
return absoluteUri.Scheme != request.Scheme
|| absoluteUri.Authority != request.Host.Value;
}
}); It still wouldn't work if the response had already started, but that's relatively uncommon. So, we have to decide:
|
Thanks, @SteveSandersonMS . I didn't realize that this was happening due to redirection to external URLs. My scenario was that I needed to do a custom redirect for login/logout scenario to an OIDC server. I was able to confirm that I can add any MVC view or even RazorPage and it works without issues. Thanks so much for the quick responses. |
Thanks for confirming. I'll reopen this to track the fact there's an open question about support for this. |
PlanWe had a long discussion about this that concluded as follows:
|
I think this is the right idea. I might even consider something more dramatic like clearing the entire page and showing nothing but the error bar rather leaving the app in an unexpected state. I do think that this will be somewhat common when cookies expire even for a properly configured app in production, so it is sad It's almost tempting to change the POST to a plain GET without parameters and hope we get the redirect anyway. My guess is 99% of the time it will be an OIDC redirect and this would be equivalent to what a Razor Page form would do, but it's not fundamentally sound. We don't even know if a GET version of the endpoint exists. |
Fixed in #50551. These kinds of redirections will now just work, as long as it's not a POST request. |
Is there an existing issue for this?
Describe the bug
I have built an application that has both razor and MVC components. I have a MVC controller called Test with a default method called index. I want to browse from a razor component to this page. I added an anchor tag in the razor component Test. When I click on test, the browser is updated with the URL, but the page doesn't reload. I understand that this could be resolved by using NavigationManage with forceload =true. However, I am using Server Side Rendering for this component, so I can't call an onclick function. I also tried adding target="_self" but that doesn't seem to work either. Is there any way to forceload the page? Or, is there another recommended solution to navigate between razor and mvc components when using Server Side Rendering.
Expected Behavior
If I have a MVC controller, i should be able to navigate to it using an anchor tag that would refresh the page.
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response
The text was updated successfully, but these errors were encountered: