0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luk164
  • 657
  • 8
  • 22
  • 1
    Your main question is answered here: https://stackoverflow.com/questions/43819583/rest-query-string-vs-url-path – R J Jan 13 '21 at 22:52
  • 1
    The second half of your question "nicer way to construct URL" is answered here: https://stackoverflow.com/questions/829080/how-to-build-a-query-string-for-a-url-in-c Sanitizing your keys and values is particularly important to avoid malicious attacks etc. – R J Jan 13 '21 at 22:54
  • @RJ Thanks, those did help somewhat. So essentially I should use the parameter approach under most circumstances, correct? – Luk164 Jan 13 '21 at 22:59

1 Answers1

1

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/

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197