Jon
Jon

Reputation: 1727

Blazor Application - pass a Query String to the app on startup

I have a Blazor application how can I pass a Query String to the app on startup? I want to pre-populate a form field and query a database with the query string value on startup.

Something like this `http://www.somesite.com?Company=Google

I have seen the following - using query string in pages. But how to you accept the query string on start up?

https://www.mikesdotnetting.com/article/340/working-with-query-strings-in-blazor

Where in the app page or code - Startup.cs / Program.cs - do you check for the query string.

thx in advance

Upvotes: 4

Views: 1098

Answers (1)

user12228709
user12228709

Reputation:

This Microsoft article will explain it better than I can:

https://learn.microsoft.com/en-us/aspnet/core/blazor/routing?view=aspnetcore-3.1

Here is a summary of relevant parts.

On your page you can have this:

@page "/Users/{text}"

Then in your code create a parameter:

[Parameter]
public string Company { get; set; }

Then if you navigate to: yoursite.com/Users/Google

your parameter will be populated, and you can do any loading / or other prerendering in OnInitializedAsync method.

// this is from one of my projects
protected override async Task OnInitializedAsync()
{
    // load the Categories
    this.Categories = await HelpCategoryService.GetHelpCategoryList();
}

You can have multiple routes on the same page, so you could have:

@page "/Users/
@page "/Users/{Company:string}"

Maybe that points you in the right direction.

Upvotes: 2

Related Questions