Redirecting in blazor with parameter

前端 未结 2 1170
粉色の甜心
粉色の甜心 2020-12-20 14:50

Hello how can you redirect to another page in Blazor with a parameter?

@page \"/auth\"
@using Microsoft.AspNetCore.Blazor.Services;
@inject Auth         


        
2条回答
  •  借酒劲吻你
    2020-12-20 15:11

    Do this:

    • Create a home.cshtml file page like this: Note that two @page directive are employed since optional parameters are not supported yet. The first permits navigation to the component without a parameter. The second @page directive takes the {username} route parameter and assigns the value to the Username property.

    Pages/home.cshtml

    @page "/home"
    @page "/home/{username}"
    
    

    @Username is authenticated!

    @functions { // Define a property to contain the parameter passed from the auth page [Parameter] private string Username { get; set; }; }
    • Do this in your auth.cshtml
        @functions{
    
            public string Username { get; set; }
            public string url = "/home";
    
            public async Task AuthAsync()
            {
                var ticket=await this.auth.AuthenticateAsync(Username);
                // Attach the parameter to the url
                urihelper.NavigateTo(url + "/" + Username); 
            }
        }
    

    Hope this helps...

提交回复
热议问题