Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Bind query parameters to a model in ASP.NET Core

I am trying to use model binding from query parameters to an object for searching.

My search object is

[DataContract]
public class Criteria 
{
  [DataMember(Name = "first_name")]
  public string FirstName { get; set; }
}

My controller has the following action

[Route("users")]
public class UserController : Controller 
{
  [HttpGet("search")]
  public IActionResult Search([FromQuery] Criteria criteria)
  {
    ...
  }
}

When I call the endpoint as follows .../users/search?first_name=dave the criteria property on the controller action is null. However, I can call the endpoint not as snake case .../users/search?firstName=dave and the criteria property contains the property value. In this case Model Binding has worked but not when I use snake_case.

How can I use snake_case with Model Binding?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You need to add [FromQuery] attribute to the model properties individually

public class Criteria
{
  [FromQuery(Name = "first_name")]
  public string FirstName { get; set; }
}
about 4 years ago · Santiago Trujillo Report

0

Solution for .net core 2.1, 2.2, 3.0 and 3.1

Or without attributes you can do something like this which is cleaner I think (of course if the model properties are same as query parameters).

Meanwhile I use it in .net core 2.1, 2.2 and 3.0 preview & 3.1.

public async Task<IActionResult> Get([FromQuery]ReportQueryModel queryModel) 
{ 

}
about 4 years ago · Santiago Trujillo Report

0

For anyone that got here from search engine like me:

To make it work on asp.net core 3.1+

public async Task<IActionResult> Get([FromQuery] RequestDto request);

public class RequestDto
{
  [FromQuery(Name = "otherName")]
  public string Name { get; set; }
}

Will read json property otherName into RequestDto.Name so basically you have to use FromQuery in 2 places. Above answers are IMHO too complicated for such a simple thing already provided in asp.net framework.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!