FromQuery deserializer requires empty constructor
See original GitHub issueHi guys 🚀
Describe the bug
As in the title FromQuery requires empty constructor. It is weird because I can use constructors (which initialise all properties) for [FromBody].
System.InvalidOperationException: Could not create an instance of type 'FancyQuery'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'query' parameter a non-null default value.
To Reproduce
Steps to reproduce the behavior:
- ASP.NET Core 2.2
- Controller and DTO/Command
// Controller method
[HttpGet("fancy")]
public async Task<IActionResult> Find([FromQuery] FancyQuery query)
{
// TODO: add some action
return Ok();
}
// DTO
public class FancyQuery
{
public FancyQuery(int userId, int dumbValue)
{
UserId = userId;
DumbValue = dumbValue;
}
public int UserId { get; set; }
public int DumbValue { get; set; }
}
Expected behavior
It should works like [FromBody].
Additional context
Add any other context about the problem here.
Include the output of dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.2.106
Commit: aa79b139a8
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.14
OS Platform: Darwin
RID: osx.10.14-x64
Base Path: /usr/local/share/dotnet/sdk/2.2.106/
Host (useful for support):
Version: 2.2.4
Commit: f95848e524
.NET Core SDKs installed:
2.2.106 [/usr/local/share/dotnet/sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
FromQuery deserializer requires empty constructor #9781
As in the title FromQuery requires empty constructor. It is weird because I can use constructors (which initialise all properties) for [FromBody] ...
Read more >c# - class can not do serialization without empty constructor
1 Answer. On deserialization XmlSerializer needs to create an object of your class, and then set its attributes one-by-one from the XML. In ......
Read more >Receive complex object FromQuery in .Net 6.0 Web API
First, read only properties cannot be deserialized. Second, either remove the constructor or add a parameterless constructor.
Read more >Model binding for query parameters encoded as JSON in ASP ...
If we try to send the parameter as JSON we can't get it to bind to a complex type. SearchQuery query parameter is...
Read more >How to use parameter binding in minimal APIs in ASP.NET ...
Minimal APIs in ASP.NET Core 7 offer several types of parameter binding including FromQuery, FromRoute, FromHeader, and FromBody.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I can confirm this happens, and it is really frustrating. When you work with DDD, you don’t want people to instantiate your objects with a “default constructor” as this facilitates code corruption. I think the behaviour should be the same as [FromBody]. Is it possible for us to suggest this? Maybe in a future release this can be changed. Thank you.
This is by design.
FromBodyuses serializers such as Newtonsoft.Json which have a different feature set compared to model binding. Model binding - indicated byFromQuery,FromRouteetc, requires that complex types have a parameterless constructor.