Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 757
Jon Avatar asked Oct 27 '25 23:10

Jon


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!