Suppose I have below URL template in my API:
/obj/execute/{compositeIdOne:int:required}/{compositeIdTwo:int:required}
Which is mapped to the following controller action
public async Task<ActionResult> SomeTask(RequestObject requestObj, CancellationToken ct = default) { ... }
With below request body:
{
"status": "string",
"name": "string",
"amount": 10
}
Now suppose I want both the request body and route parameters inserted into the RequestObject
parameter specified. Of course I could supply the ids in the route as separate parameters and then set the properties of the object in the function body, but that feels ugly.
Would I be able to specify the parameter location in the class definition and have .NET autofill the object that way.
In example:
public class RequestObject
{
[FromRoute("compositeIdOne")]
public int IdOne { get; set; }
[FromRoute("compositeIdTwo")]
public int IdTwo { get; set; }
[FromBody("status")]
public string Status { get; set; }
[FromBody("name")]
public string Name { get; set; }
[FromBody("amount")]
public int Amount { get; set; }
}
I realise this may be a very abstract use case, however it would make for much cleaner controllers as well as make the API much cleaner to consume.
Any other suggestions to achieve this are of course appreciated.
Thank you in advance!