@@ -7,6 +7,10 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage
77{
88 public const string StatusCookieName = "Identity.StatusMessage" ;
99
10+ [ FeatureSwitchDefinition ( "Microsoft.AspNetCore.Components.Endpoints.NavigationManager.EnableThrowNavigationException" ) ]
11+ private static bool _throwNavigationException =>
12+ AppContext . TryGetSwitch ( _enableThrowNavigationException , out var switchValue ) && switchValue ;
13+
1014 private static readonly CookieBuilder StatusCookieBuilder = new ( )
1115 {
1216 SameSite = SameSiteMode . Strict ,
@@ -15,7 +19,6 @@ internal sealed class IdentityRedirectManager(NavigationManager navigationManage
1519 MaxAge = TimeSpan . FromSeconds ( 5 ) ,
1620 } ;
1721
18- [ DoesNotReturn ]
1922 public void RedirectTo ( string ? uri )
2023 {
2124 uri ??= "" ;
@@ -26,21 +29,22 @@ public void RedirectTo(string? uri)
2629 uri = navigationManager . ToBaseRelativePath ( uri ) ;
2730 }
2831
29- // During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect.
30- // So as long as this is called from a statically rendered Identity component, the InvalidOperationException is never thrown.
3132 navigationManager . NavigateTo ( uri ) ;
32- throw new InvalidOperationException ( $ "{ nameof ( IdentityRedirectManager ) } can only be used during static rendering.") ;
33+ if ( _throwNavigationException )
34+ {
35+ // During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect.
36+ // So as long as this is called from a statically rendered Identity component, the InvalidOperationException is never thrown.
37+ throw new InvalidOperationException ( $ "{ nameof ( IdentityRedirectManager ) } can only be used during static rendering.") ;
38+ }
3339 }
3440
35- [ DoesNotReturn ]
3641 public void RedirectTo ( string uri , Dictionary < string , object ? > queryParameters )
3742 {
3843 var uriWithoutQuery = navigationManager . ToAbsoluteUri ( uri ) . GetLeftPart ( UriPartial . Path ) ;
3944 var newUri = navigationManager . GetUriWithQueryParameters ( uriWithoutQuery , queryParameters ) ;
4045 RedirectTo ( newUri ) ;
4146 }
4247
43- [ DoesNotReturn ]
4448 public void RedirectToWithStatus ( string uri , string message , HttpContext context )
4549 {
4650 context . Response . Cookies . Append ( StatusCookieName , message , StatusCookieBuilder . Build ( context ) ) ;
@@ -49,10 +53,8 @@ public void RedirectToWithStatus(string uri, string message, HttpContext context
4953
5054 private string CurrentPath => navigationManager . ToAbsoluteUri ( navigationManager . Uri ) . GetLeftPart ( UriPartial . Path ) ;
5155
52- [ DoesNotReturn ]
5356 public void RedirectToCurrentPage ( ) => RedirectTo ( CurrentPath ) ;
5457
55- [ DoesNotReturn ]
5658 public void RedirectToCurrentPageWithStatus ( string message , HttpContext context )
5759 => RedirectToWithStatus ( CurrentPath , message , context ) ;
5860}
0 commit comments