【发布时间】:2022-03-02 19:43:30
【问题描述】:
我正在尝试将 Razor Pages 应用程序迁移到 Blazor Server 应用程序作为分阶段迁移(而不是大爆炸)的一部分。我目前正在移动一组相对直接的 CRUD 页面。这些页面具有典型的路由结构,例如...
/entity/
/entity/edit/{id}
/entity/detail/{id}
/entity/delete/{id}
在“编辑”的剃须刀页面中,我通常会有一个 OnGetAsync 方法,该方法接受 id 作为参数,使用 id 搜索实体并返回页面。但是,如果 id 不是有效实体,我的 OnGet 将返回 NotFound (IActionResult)。我想弄清楚的是如何使用 Blazor 服务器页面/组件来做到这一点。
我的 blazor 页面看起来像这样(只是相关的部分)
@page "/entity/edit/{id:int}"
...
@code {
[Parameter] public int Id { get; set; }
public Entity Model { get; set; }
protected override async Task OnParametersSetAsync()
{
Model = await SomeServiceCall.GetAsync(Id);
if(Model == null)
return; // TODO: Return 404 rather than the page
}
}
如何告诉 Blazor 返回 404?这样显示的是我的而不是我的页面?
【问题讨论】:
-
这个answer 似乎提供了一个解决方案。
标签: asp.net-core blazor razor-pages