I've a Blazor WASM (hosted) Application. Everything is working fine. Except for the Situation if I manually enter a URL to a page.
For ex. I've a page "RsetPw2.razor".
The code is as follows:
@page "/Auth/respw2"
@using Microsoft.AspNetCore.WebUtilities
@using Microsoft.Extensions.Primitives;
@layout AccountLayout
@inject IAuthService AuthService
@inject ISnackbar Snackbar
@inject NavigationManager navMgr
@if (usTok == null || usEm == null)
{
<MudContainer>
<MudCard style="margin-top:60px; width:80%;" Elevation="5" Class="pa-auto ml-auto mr-auto">
<MudCardHeader>
<MudText Typo="Typo.h5">Reset your Password (step two)</MudText>
</MudCardHeader>
<MudCardContent>
<MudAlert Severity="Severity.Error" Variant="Variant.Filled">This page can not be called without a valid token and a valid user email address!</MudAlert>
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Disabled="true" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Register</MudButton>
</MudCardActions>
</MudCard>
</MudContainer>
}
else
{
<EditForm Model="@user" OnValidSubmit="@HandleReset">
<DataAnnotationsValidator />
<MudCard style="margin-top:60px; width:80%;" Elevation="5" Class="pa-auto ml-auto mr-auto">
<MudCardHeader>
<MudText Typo="Typo.h5">Reset your Password</MudText>
</MudCardHeader>
<MudCardContent>
<MudTextField Label="Email address" Disabled="true" HelperText="Max. 8 characters" @bind-Value="@usEm" For="@(() => user.UserEmail)" />
<MudTextField Label="Reset token" Disabled="true" Class="mt-3" HelperText="the token you'vet got by mail'" @bind-Value="@usTok" For="@(() => user.ResetToken)" />
<MudTextField Label="New Password" HelperText="Choose a strong password" Class="mt-3" @bind-Value="@user.NewPassword" For="@(() => user.NewPassword)" InputType="InputType.Password" />
<MudTextField Label="repeat new Password" HelperText="Repeat the password" Class="mt-3" @bind-Value="@user.ConfirmPassword" For="@(() => user.ConfirmPassword)" InputType="InputType.Password" />
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Reset Password</MudButton>
</MudCardActions>
</MudCard>
</EditForm>
}
@code {
private UserPwResetModel user = new UserPwResetModel();
private StringValues tok = String.Empty;
private StringValues em = String.Empty;
private string usTok { get; set; } = String.Empty;
private string usEm { get; set; } = String.Empty;
protected override void OnInitialized()
{
var uri = navMgr.ToAbsoluteUri(navMgr.Uri);
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("token", out tok))
{
usTok = Convert.ToString(tok);
}
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("um", out em))
{
usEm = Convert.ToString(em);
}
}
private async Task HandleReset()
{
user.ResetToken = usTok;
user.UserEmail = usEm;
var res = await AuthService.ResetPassword(user);
if (!res.Success)
{
snackMessage(res.Message, Severity.Error, Defaults.Classes.Position.BottomRight);
}
else
{
snackMessage(res.Message, Severity.Success, Defaults.Classes.Position.BottomRight);
var Count = 3;
var tmr = new System.Threading.Timer(new TimerCallback(_ =>
{
if (Count > 0)
{
Count--;
}
if(Count == 0)
{
Snackbar.Clear();
navMgr.NavigateTo("/Auth/login");
}
}), null, 1000, 1000);
}
}
// show a snackbar message
private void snackMessage(string message, MudBlazor.Severity type, string position)
{
Snackbar.Clear();
Snackbar.Configuration.PositionClass = position;
Snackbar.Add(message, type);
}
}
When I now enter the URL https://localhost:7076/Auth/respw2
, I'm being automatically redirected to https://localhost:7076
.
If I navigate to the URL within the Application using NavigationManager.NavigateTo
, the page works without any hassle.
I have really no idea, where and why the redirect occurs.
I hope someone has an idea...
PS: I forgot to mention: I've tested that with a blank Blazor WASM(hosted) Application. And there I can manually enter a URL to an existing page. So it must be a problem with my application :-(
CodePudding user response:
so, after a long time of testing. I found no reason for this this problem!
I've solved it by doing the following:
- create an ne empty Blazor WASM (asp.net core hosted) project
- delete all components, services and controllers
- copy the pages, components, services and controller from the non working project and paste them into the new blank project.
The routing works by manually entering a url. Very strange...