[FromQuery] Model binding property is filled with a random value when query doesn't contain the property
area-mvcNeeds: Attention :wave:
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
In a dotnet9 web application, configured with `AddControllersWithViews`.
```C#
//In HomeController
[HttpGet("GetRequest")]
public IActionResult GetRequest([FromQuery]string number, [FromQuery]RequestId requestId)
{
Debug.WriteLine("number = " + number);
Debug.WriteLine("RequestId = " + requestId);
return View("Request", requestId);
}
//RequestId
public readonly record struct RequestId : IParsable<RequestId>
{
public Guid Guid { get; } = Guid.Empty;
public RequestId(Guid guid)
{
Guid = guid;
}
public RequestId()
{
Guid = Guid.NewGuid();
}
public override string ToString()
{
return Guid.ToString();
}
public static implicit operator RequestId(Guid id)
{
Debugger.Break();
return new RequestId(id);
}
public static RequestId Default { get; } = new RequestId(Guid.Empty);
public static RequestId Parse(string s, IFormatProvider? provider)
{
//Debugger.Break();//This is never triggered in incorrect Case
return new RequestId(Guid.Parse(s, provider));
}
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out RequestId result)
{
//Debugger.Break();//This is never triggered in incorrect Case
if (!Guid.TryParse(s, provider, out Guid guid))
{
result = Empty;
return false;
}
result = new RequestId(guid);
return true;
}
public static RequestId Empty { get; } = new(Guid.Empty);
}
```
The issue is, if you request `http://localhost:5249/GetRequest?number=12345`, although the `requestId` parameter is missing, the output will print a random Guid. But I expects it will be a zero Guid or just throw an exception.
In my production project, I use Zero Guid to detect if the parameter is valid, I can't know if it is valid when it is random.
### Expected Behavior
1. throw an exception/error
2. requestId is the default value of RequestId, which should be a empty Guid string with all zeros
### Steps To Reproduce
Just run the web app and open the home page in browser, then click the link under Case02.
When `number` is provided and `requestId` is not provided:
http://localhost:5249/GetRequest?number=12345
but the result is that requestId is filled with a random Guid value.
**minimal reproducible example project**:
https://github.com/zwcloud/aspnetcore-issue-repo-model-property-filled-with-random-value/
**video**:
https://github.com/user-attachments/assets/154f6df3-2cc4-44aa-8907-790c6db0c25d
### Exceptions (if any)
_No response_
### .NET Version
10.0.204
### Anything else?
Output of `dotnet --info`:
```text
.NET SDK:
Version: 10.0.204
Commit: e7aa4b537d
Workload version: 10.0.200-manifests.b47d7e23
MSBuild version: 18.3.3+e7aa4b537
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.204\
.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 10.0.8
Architecture: x64
Commit: 94ea82652c
.NET SDKs installed:
8.0.422 [C:\Program Files\dotnet\sdk]
9.0.315 [C:\Program Files\dotnet\sdk]
10.0.204 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
DOTNET_CLI_UI_LANGUAGE [en]
```
2 条评论