dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.21k stars 9.95k forks source link

Different model binding behaviour for nullable query params with default value in asp net core #57705

Open mnerubashchenko opened 2 weeks ago

mnerubashchenko commented 2 weeks ago

Is there an existing issue for this?

Describe the bug

Hello! I noticed different behaviour in Model Binding for nullable query parameters with default value after upgade from .net 6 to 7 version. I will show this by examples.

I have 2 applications, one of them using .net v6, another one - .net v7. I will send the same request to both of applications. This is the request I will send - WeatherForecast?a=.

Endpoint in first application with .net 6:

Снимок экрана 2024-09-05 в 12 12 44

Endpoint in second application with .net 7:

Снимок экрана 2024-09-05 в 12 12 35

As you can see there are different behaviours after upgrade version of .net. I was trying to find any information about this change in many resource included in Microsoft articles about breaking changes. But I can't find. Maybe this is a bug?

Can you explain this and how can I fix it in my app. Now I have a couple options: rewrite GET to POST and use body instead of query params (it works). And may be use custom model binder (not tried yet).

Thanks in advance!

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

captainsafia commented 1 week ago

@mnerubashchenko Thanks for filing this issue!

I believe this behavior you are seeing is a result of a change that we introduced in .NET 7 to prioritize the new TryParseModelBinder in MVC over the pre-existing TryParseConverters. We received several bug reports at the time and eventually reverted this behavior in .NET 8.

Can you try this in .NET 8 and see if you repro the same issue?

As a side note: .NET 7 is out-of-support at the moment so I'd recommend upgrading from .NET 6 to .NET 8 anyways. 😄

dotnet-policy-service[bot] commented 1 week ago

Hi @mnerubashchenko. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

dotnet-policy-service[bot] commented 3 days ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.