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

292
Views
Combine [FromBody] and [FromRoute] attributes in one model

I am trying to create model, which I could use for Body and Route at once. I found solution here, but I would like to move it little bit further. Not using extra "body" class, but include everything in one class. I tried this, but it does not work.

    public class Car
    {
        [Required, FromRoute]
        public int CarId { get; set; }

        [Required, FromBody]
        public string UpdatedBy { get; set; }

        [Required, FromBody]
        public string Colour { get; set; }

        [Required, FromBody]
        public string Wheels { get; set; }
      
        [Required, FromBody]
        public DateTime UpdatedDate { get; set; }
    }

I am trying to use it as input for PUT method:

public async Task<IActionResult> CarUpdate(Car car, CancellationToken cancellationToken)

enter image description here

But I would like to have it like this:

enter image description here

Is it possible? Thank you.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Unfortunately you can only use FromBody once, you are trying to use it multiple times , this is why is not working. This looks akward but will be working

public async Task<IActionResult> CarUpdate(CarRoute carRoute, CancellationToken cancellationToken)

but you will have to add this to startup

services.Configure<ApiBehaviorOptions>(options =>
{
    options.SuppressInferBindingSourcesForParameters = true;
});

classes

 public class CarRoute
    {
        [FromRoute]
        public int CarId { get; set; }
        [FromBody]
        public Car Car { get; set; }
    }
    public class Car
    { 
        public int CarId { get; set; }
        public string Name { get; set; }
        public string Colour { get; set; }
    }
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!