HTK
HTK

Reputation: 1

Razord Page specifying Parameter and SupplyParameterFromQuery for Property gives null result for Parameter

There are 2 routes to my .NET 8 razor page:

  1. https://localhost:7215/DemoForm/30030443-2b0c-4b94-959d-fb6132dc164f

  2. https://localhost:7215/DemoInfo?oid=30030443-2b0c-4b94-959d-fb6132dc164f

Demo.razor:

@page "/DemoForm/{rowguid:guid}"
@page "/DemoInfo"

@rendermode InteractiveServer

<h3>Demo</h3>
<h1>Rowguid is @Rowguid</h1>

@code {
    [Parameter, SupplyParameterFromQuery(Name = "oid")]
    public Guid Rowguid { get; set; }
}

I expect both results to be:

Rowguid is 30030443-2b0c-4b94-959d-fb6132dc164f

However, URL 1 returns:

Rowguid is 00000000-0000-0000-0000-000000000000

URL 2 returns the expected result

Rowguid is 30030443-2b0c-4b94-959d-fb6132dc164f

If I change Demo.razor to

@page "/DemoForm/{rowguid:guid}"
@page "/DemoInfo"

@rendermode InteractiveServer

<h3>Demo</h3>
<h1>Rowguid is @Rowguid</h1>
<h1>_oid is @_oid</h1>

@code {
    [Parameter]
    public Guid Rowguid { get; set; }

    [SupplyParameterFromQuery(Name = "oid")]
    public Guid? _oid { get; set; }

    protected override Task OnInitializedAsync()
    {
        Rowguid = _oid ?? Rowguid;

        return base.OnInitializedAsync();
    }
}

Rowguid will have the correct results for both URLs.

So what am I missing here?

Upvotes: 0

Views: 29

Answers (1)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30310

I'm pretty sure the parameter assignment happens first. The query string will always write over the parameter value.

So in:

[Parameter, SupplyParameterFromQuery(Name = "oid")]
    public Guid Rowguid { get; set; }

and with url:

https://localhost:7215/DemoForm/30030443-2b0c-4b94-959d-fb6132dc164f

There's no query string value provided, so you get the default Guid.Empty assigned.

Make Rowguid nullable:

    <h1>Rowguid is @(Rowguid?.ToString() ?? "null")</h1>

@code {
    [SupplyParameterFromQuery(Name = "oid"), Parameter]
    public Guid? Rowguid { get; set; }
}

And you get null, the default value for Guid?.

You need to handle the two separately as you do in the second approach.

Upvotes: 0

Related Questions