我有一个Blazor WASM(托管(应用程序。一切都很好。如果我手动输入页面的URL,则情况除外。
对于前任,我有一页";RsetPw2.剃刀">
代码如下:
@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);
}
}
当我现在输入URLhttps://localhost:7076/Auth/respw2
时,我会被自动重定向到https://localhost:7076
。
如果我使用NavigationManager.NavigateTo
导航到应用程序中的URL,则该页面工作起来不会有任何麻烦。
我真的不知道重定向发生在哪里以及为什么。
我希望有人有个主意。。。
附言:我忘了提一下:我已经用一个空白的Blazor WASM(托管(应用程序测试过了。在那里,我可以手动输入现有页面的URL。所以这一定是我的应用程序的问题:-(
所以,经过长时间的测试。我找不到这个问题的原因!
我通过以下操作解决了这个问题:
- 创建一个空的Blazor WASM(asp.net核心托管(项目
- 删除所有组件、服务和控制器
- 从非工作项目中复制页面、组件、服务和控制器,并将它们粘贴到新的空白项目中
路由通过手动输入url来工作。很奇怪。。。