Blazor在.NET8中引入了在服务器上执行的选项(所谓的静态模式).它可以接受表格帖子,这很棒.但在那之后,我应该如何导航到另一个页面呢?如果我调用NavigationManager.NavigatoTo,则会得到Microsoft.AspNetCore.Components.NavigationException
有没有其他方法可以将用户重定向到另一个页面?
一些示例代码
namespace DemoBlazorWebApp.Client.Pages;
using DemoBlazorWebApp.Dtos;
using Microsoft.AspNetCore.Components;
public partial class ProductCreate
{
[Inject]
private NavigationManager NavigationManager { get; set; } = null!;
[SupplyParameterFromForm]
private ProductCreateDto Model { get; set; } = new();
private bool IsSaving { get; set; }
private async Task SubmitAsync()
{
IsSaving = true;
try
{
ProductDto? product = await //save the product somehow
NavigationManager.NavigateTo("/Products/" + product.ProductId);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
IsSaving = false;
}
}
}
@page "/Products/Create"
<h3>Product Create</h3>
@if (IsSaving)
{
<p>
Saving...
</p>
}
else
{
<EditForm FormName="ProductCreate" Model="@Model" OnValidSubmit="SubmitAsync">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label>
</label>
</p>
<p>
<label>
Name:
<InputText @bind-Value="Model.Name" />
</label>
</p>
<p>
<label>
Price:
<InputNumber @bind-Value="Model.Price"></InputNumber>
</label>
</p>
<p>
<label>
Description:
<InputTextArea @bind-Value="Model.Description" />
</label>
</p>
<button type="submit">Save</button>
</EditForm>
}
请注意,托管页面的路径应采用新的默认静态模式(即未指定其他渲染模式),并且其中一条路径应使用新的Web应用程序脚本