如何在 Razor Page OnInitialized 事件中使用 404 路由 [英] How to use 404 routing in Razor Page OnInitialized event

查看:23
本文介绍了如何在 Razor Page OnInitialized 事件中使用 404 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器端 Blazor 应用程序(Core 3.1)中有一个 Razor,它接受 @page 属性中的标识符.如果 URL 中提供的标识符对应于现有实体,则页面将正常呈现.但是,如果标识符不是已知实体(由其在存储库中的存在确定),我希望系统执行与 404 Not Found 对应的任何操作.然而,我不知道这一点,直到路由已经匹配并且我的页面的 OnInitialized() 正在执行.

In a server-side Blazor application (Core 3.1) have a Razor that accepts an identifier in the @page attribute. If the identifier supplied in the URL corresponds to an existing entity, the page will render fine. However, if the identifier is not a known entity (as determined by its presence in the repository) I would like the system to perform whatever action corresponds to 404 Not Found. I don't know this, however, until the route has already been matched and my page's OnInitialized() is executing.

在这种情况下,我如何重定向"到默认的 404 处理.

How can I "redirect" to the default 404 handling in this case.

页面如下所示:

@page "/{projectname}"

<!-- HTML Here -->

@code {


    [Parameter]
    public string ProjectName {get; set;}

    private UpdateProjectViewModel Project;

    protected override void OnInitialized()
    {
        var project = Repository.Get(ProjectName);
        if (project == null)
        {
            WANT TO USE 404 ROUTING HERE.
        }
        Project = new UpdateProjectViewModel(project));
    }

}

推荐答案

这里是代码片段

@page "/navigate"
@inject NavigationManager NavigationManager

<h1>Navigate in Code Example</h1>

<button class="btn btn-primary" @onclick="NavigateToCounterComponent">
    Navigate to the Counter component
</button>

@code {
    private void NavigateToCounterComponent()
    {
        NavigationManager.NavigateTo("404");
    }
}

这篇关于如何在 Razor Page OnInitialized 事件中使用 404 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆