1

I already know that I could use the [SupplyParameterFromQuery(Name = "status")] from a URL parameter in Blazor.

To get the value from the URL parameter using the given attribute, I had to specify the query string parameter (such as status) in the attribute code. Doing this prevents an exception when retrieving the correct parameter.

However, I have no existing issue (in Example number 1), and I want to understand this attribute compared to [Parameter].


Currently, I am only using the [SupplyParameterFromQuery], for example:

Example number 1

List of records page

<Tooltip Class="d-flex justify-content-center" Title="Review" Placement="TooltipPlacement.Top"> <a class="btn btn-outline-primary btn-sm" href="@($"users/review-user?status={IsVerified(a.fBool, a.sBool):bool}&row={a.Hash}&userid={a.UserID}")"> <Icon Name="IconName.PencilSquare" /> </a> </Tooltip> 

Note that a.fBool, a.sBool, a.Hash, and a.UserID came from a data class model, but technically came from @foreach (var a in SortedUserList) {...}

Single page to review a specific record

[SupplyParameterFromQuery(Name = "status")] private bool status { get; set; } [SupplyParameterFromQuery(Name = "row")] private int rowNum { get; set; } [SupplyParameterFromQuery(Name = "userid")] private string? id { get; set; } 

However, I have found out that a.Hash is declared as long for some reason. Therefore, I needed to refactor the previously stated code snippets.

Refactored:

Example number 2

List of records page

<Tooltip Class="d-flex justify-content-center" Title="Review" Placement="TooltipPlacement.Top"> <a class="btn btn-outline-primary btn-sm" href="@($"users/review-user?status={IsVerified(a.fBool, a.sBool):bool}&row={a.Hash:long}&userid={a.UserID}")"> <Icon Name="IconName.PencilSquare" /> </a> </Tooltip> 

Single page to review a specific record

[SupplyParameterFromQuery(Name = "status")] private bool status { get; set; } [SupplyParameterFromQuery(Name = "row")] private long rowNum { get; set; } [SupplyParameterFromQuery(Name = "userid")] private string? id { get; set; } 

Unfortunately, I got an error:

InvalidOperationException: Cannot parse the value 'long' as type 'System.Int64' for 'row'.

This time, I don't get why I am getting an exception since I define the parameter based on its origin data types.


On the other hand, from this thread: How to pass a URL input parameter value to Blazor page?'s answer, which I'll quote:

@page "/counter/{start:int}" <h1>Counter</h1> <p>Current count: @Start</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> @code { [Parameter] public int Start { get; set; } private void IncrementCount() { Start++; } } 

Note that I deliberately excluded the @page directive that has no parameter instance.


I have a lack of time to deep dive into these two attributes: [SupplyParameterFromQuery] and [Parameter].

Especially to test its behavior in a program while handling specific data types such as string?, int, long, bool, etc.

Since this thread is primarily questioning the [SupplyParameterFromQuery] vs [Parameter] in Blazor...


To extend this primary question (to make it narrower and specific):

  • What are the major differences between the two?
  • Do they offer different behaviors?
  • When should I use one over the other?

P.S., I've already resolved the exception from example number 2. I had to remove :long after a.Hash while leaving the SupplyParameterFromQuery attribute as is. I've left it this way because I can't technically explain why that fixed the issue.

1
  • 1
    Short version for the exception: $"{1:long}" . It's just not a valid interpolation. Commented Aug 22 at 7:01

1 Answer 1

0

One main difference is in nullability. A query parameter is inherently optional. A normal (path) parameter is required and the only way to get a null is multiple @page directives.

@page "/products" // otherwise: notfound @page "/products/{id:long}" [Parameter] public long? Id { get; set; } 

Or, with an optional ?row=123 in any path URL:

[SupplyParameterFromQuery(Name = "row")] private long rowNum { get; set; } = 0; // set a default or make it nullable 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.