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

504
Views
How to use [FromBody] and [FromQuery] attribute at the same time?

In the post request,I need to get the parameters from the query string at the same time.This is my code,but can't work


 [HttpPost("test")]
 public string Test(TestRequest request)
 {
      //TODO  ...
 }


    public class TestRequest
    {
        [FromHeader(Name = "id")]
        public string Id { get; set; }

        [FromQuery(Name = "traceId")]
        public string Trace { get; set; }

        public string Name { get; set; }

        [MaxLength(4)]
        public string Mark { get; set; }

        [Range(18, 35)]
        public int Age { get; set; }
    }

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have to put the different parameters in the method signatur, so it would be something like

[HttpPost("test/{traceId}")] // Note the query parameter 
public string Test(
    [FromHeader(Name = "id")]string id,
    [FromQuery(Name = "traceId")]string trace,
    [FromBody]Request request
    )
{
    // TODO...
}

public class Request
{
    public string Name { get; set; }
    public string Mark { get; set; }
    public int Age { get; set; }
}

bit depending on how your actual request looks like. This would read a Request-Object form the body, the id from the header and the traceId from the query.

over 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!