I am new into asp.net core webAPI. I have created a Controller method that taken a request object.
[HttpPost]
[Route("/api/DoWork")]
[ValidateModelState]
public virtual IActionResult DoWork(CustomRequest request)
{
//TODO: implement this method
}
Now this CustomRequest
calss is generic class, which have multiple properties and depending upon the client/tenant will populate/set values for some of the properties.
Example :
Suppose CustomRequest
class structure is,
public partial class CustomRequest
{
public string ReqId { get; set; }
public DateTime? BusinessDate { get; set; }
public DateTime? CurrentDate{ get; set; }
}
So Customer A can only set values (or send values in request) ReqId
and BusinessDate
and Customer B send values ReqId
and CurrentDate
. As to proceed with business operation either of the date is required.
It's also previously decided which client will send which of the values, and this number of properties is large.
So I was thinking of creating a relatively small class which will be subset of CustomRequest
class and sufficient enough to process business operations. And I can think of two options to proceed with that.
- Write code inside my controller action to create small sub set class and then call another method that expect only the subset class
- Use Custom Model binders to create that small subset class and change the method signature.
My question is:
What is the best way to handle this situation option 1 or 2 or any other option? Best way in terms of performance and customization option available (like in option 1, i don't think we can use ModelState
)
Any help or guidance...