简体   繁体   中英

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. 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.

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

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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