question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FromQuery deserializer requires empty constructor

See original GitHub issue

Hi 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:

  1. ASP.NET Core 2.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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, May 5, 2019

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.

1reaction
pranavkmcommented, May 1, 2019

This is by design. FromBody uses serializers such as Newtonsoft.Json which have a different feature set compared to model binding. Model binding - indicated by FromQuery, FromRoute etc, requires that complex types have a parameterless constructor.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found