4

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thx.. which page might you add - the following MainLayout ? page "/Users/{Id:int}" <h1>The user Id is @Id!</h1> @code { [Parameter] public int Id { get; set; } }
Whichever page you need the parameter available on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.