20

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.

2 Answers 2

22

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

5
  • How do you extract the parameter username in the home page? Commented Jan 22, 2019 at 14:33
  • 1
    You do not extract it yourself. As you can see, the home component contains a property named Username, and annotated with the Parameter attribute. The extraction and assignment to this property is automatically done by the Blazor... framework
    – enet
    Commented Jan 22, 2019 at 14:47
  • Oh i see thank you very much.But if i would want objects , would it still work (if i stringify them) ? Commented Jan 22, 2019 at 14:47
  • 1
    If you serialized the object (turning it to a JSON string), I guess it would work, but this is a very bad idea. Depending on how you structure your application, you may use events to notify an object of the occurrence of something, such as validating a user credentials, and passing this object these credentials, etc...
    – enet
    Commented Jan 22, 2019 at 18:30
  • fyi: learn.microsoft.com/en-us/aspnet/core/blazor/…
    – Neil
    Commented Feb 5, 2020 at 0:21
4

You can only pass parameters in the URL at present.

So, if your home component was expecting [Parameter] string Name you would need to provide a URL of /home/fred and fred would be passed into the Name parameter of the home component.

If you are looking to pass more complex data then you would have to look at doing it via some kind of service.

Here is the link to the official docs on routing parameters: https://blazor.net/docs/routing.html#route-parameters

Not the answer you're looking for? Browse other questions tagged or ask your own question.