简体   繁体   English

如何在 Razor Page OnInitialized 事件中使用 404 路由

[英]How to use 404 routing in Razor Page OnInitialized event

In a server-side Blazor application (Core 3.1) have a Razor that accepts an identifier in the @page attribute.在服务器端 Blazor 应用程序(Core 3.1)中,有一个 Razor 接受@page属性中的标识符。 If the identifier supplied in the URL corresponds to an existing entity, the page will render fine.如果 URL 中提供的标识符对应于现有实体,则页面将正常呈现。 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.但是,如果标识符不是已知实体(由其在存储库中的存在确定),我希望系统执行与 404 Not Found 对应的任何操作。 I don't know this, however, until the route has already been matched and my page's OnInitialized() is executing.然而,我不知道这一点,直到路由已经匹配并且我的页面的OnInitialized()正在执行。

How can I "redirect" to the default 404 handling in this case.在这种情况下,我如何“重定向”到默认的 404 处理。

The page looks like this:该页面如下所示:

@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));
    }

}

Here is the code snippet这是代码片段

@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");
    }
}

In case you want to move to the error page.如果您想移动到错误页面。 Create error page component创建错误页面组件

@page "/NotFound"

<h5>The resource you requested was not found</h5>

And then from your OnInitialized()然后从你的 OnInitialized()

    @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)
        {
            NavigationManager.NavigateTo("/NotFound");        
        }
        Project = new UpdateProjectViewModel(project));
    }
}

In case you want to remain on the same page如果您想保持在同一页面上

@page "/{projectname}"
@if(!loading){
@if(validResource)
{
   // show resource state
}else
{
    <h5>The resource you requested was not found</h5>
}
}
@code {


    [Parameter]
    public string ProjectName {get; set;}
    bool isValidResource;
    bool isLoading;
    private UpdateProjectViewModel Project;

    protected override void OnInitializedAsync()
    {
        loading = true;
        await Task.Run(()=>
        var project = Repository.Get(ProjectName);
        if (project == null)
        {
           isValidResource = false;
        }else
        {
           isValidResource = true;
          Project = new UpdateProjectViewModel(project));
        }
        loading = false;
      }
      
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM