I have a asp.net core (v2.1) webapi project which expose this function:
[HttpPost]
[Route("v1/do-something")]
public async Task<IActionResult> PostDoSomething(ModelData model)
{
//...
}
and this model:
public class ModelData
{
[Required]
public string Email { get; set; }
}
I want to make this endpoint flexible, in content type perspective. Therefore, it should be OK to send this endpoint diffrent content type in body.
For example, those "BODY" argument will be allowed:
// application/x-www-form-urlencoded
email="abc123@gmail.com"
// application/json
{
"email": "abc123@gmail.com"
}
In contrast to the old .net framework, in dotnet core this is not allowed out of the box. I found that I need to add Consume attribute with [FormForm] attribute. But, If I add the [FormForm] attribute the the model argument, it wont work anymore on JSON (for example) - because then it should be [FromBody].
I though that it will be ok to use code like this:
[HttpPost]
[Route("v1/do-something")]
public async Task<IActionResult> PostDoSomething([FromBody] [FromForm] ModelData model)
{
//...
}
But as you can expect, this code not working.
So, in order to achieve this flexibility I have to duplicate all my endpoint - which sound like a very very bad idea.
[HttpPost]
[Route("v1/do-something")]
[Consume ("application/json")]
public async Task<IActionResult> PostDoSomething([FromBody] ModelData model)
{
//...
}
[HttpPost]
[Route("v1/do-something")]
[Consume ("application/x-www-form-urlencoded")]
public async Task<IActionResult> PostDoSomething([FromForm] ModelData model)
{
//...
}
// ... Other content types here ...
It's sound an easy task. But seems like more complicated.
I missed something? How to make an endpoint work in any content type?