简体   繁体   中英

Blazor routing with required and optional parameters

I am a bit confused about blazor routing. One of the ways we can send a parameter in a route would be @page "/FulfillOrder/{id:int}" with a corresponding [Parameter] in the code section. The URL would look like "mypage.com/FulfillOrder/5".

I can also use query string "mypage.com/FulfillOrder/5?ship=Tardis" to add parameter and get the ship value via QueryHelpers .

My question is, why why and when should I use parameters/query string? When to use both? Are these two the only way or are there more?

Also kinda unrelated but is there a nicer way to construct the URL than just using interpolated strings and a NavLink component?

Optional parameters aren't supported. However, we can use two @page directives to get the effect of optional parameters.

@page "/fullfillOrder"
@page "/fullfillOrder/{Id}"

The first @page directive allows navigation to the component without a parameter. The second @page directive receives the {Id} route parameter and assigns the value to the Id public property on the component class.

Reference :https://www.pragimtech.com/blog/blazor/blazor-route-parameters/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM