Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

c# - Redirecting in blazor with parameter

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

@page "/auth"
@using Microsoft.AspNetCore.Blazor.Services;
@inject AuthService auth
@inject IUriHelper urihelper;

<input type="text" bind="@Username" />
<button onclick="@AuthAsync">Authenticate</button>



@functions{

    public string Username { get; set; }
    public string url = "/home";

    public async Task AuthAsync()
    {
        var ticket=await this.auth.AuthenticateAsync(Username);
        urihelper.NavigateTo(url); //i see no overload that accepts parameters
    }
}

In this case i want to navigate to the /home page giving it a string as parameter.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}"

<h1>@Username is authenticated!</h1>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...