问题
I am developing a Blazor Server Side Application which I need to include cookie authentication. We created a custom login page component, but in order to create a cookie I need to call a Razor page to create the cookie.
Is there a way to call the razor page without doing a page refresh.
I tried using the UriHelper.NavigateTo but does not work with non Blazor component routes.
Then I tried with a Javascript call to do a window.location, but the issue is that this causes a browser refresh that makes all my instances to be recreated (like AppState, HttpClient), so all the information in these instances are lost (like httpclient headers, etc).
I expect to call the razor page to create a cookie, in a way that the httpclient and appstate scoped instance keep their values.
回答1:
We tried the Identity scaffolding but it has a bug in the latest Preview 8
I've seen that bug, easily corrected. You can scaffold now. And otherwise,
the only problem that we have is that we could not find a way to customize the bland Login and Register pages.
maybe you could live with those 'bland pages' until the release, end of September?
The steps, based on a Preview8 server-side project with "Individual user accounts"
NB: it is a good idea to Commit-changes in Git before and after this
- temporarily disable this line in Startup:
// endpoints.MapBlazorHub<App>(selector: "app");
- run the scaffolding wizzard, add any pages you want
- uncomment the
endpoints.MapBlazorHub()
line again - remove
Pages/_ViewStart.cshtml
- fix
Pages/Shared/_Layout.cshtml
, the first line is missing an@using
- fix
Pages/Shared/_Loginpartial.cshtml
, the second line is missing@inject
Compile and Run
回答2:
I tried using the UriHelper.NavigateTo but does not work with non Blazor component routes.
Not true. NavigateTo gets two parameter: the first is the uri, the second a boolean, named forceLoad, If true, it bypasses client-side routing and forces the browser to load the new page from the server, whether or not the URI would normally be handled by the client-side router. In other words it is executed in the JavaScript portion of Blazor on window.location.
Conclusion: Not possible... Once you attempt to go beyond the limits of the router, a page refresh is inevitable. Remember that the Blazor app is rendered on a single page, and thus, once you navigate to a new page, the previous page expires.
Hope this helps...
来源:https://stackoverflow.com/questions/57662442/how-to-call-a-razor-page-from-a-blazor-component-in-a-server-side-blazor-applica