One of my web api project -uses .NetCore 2.1- i want to have a convention of returning messages. All successful result objects must be enveloped with a result object and all errors and such things must leave result null and fill the resultCode and resultMessage fields.
Should i do this on startup.cs with formatters or create a response builder class and not use [ApiController] attribute tag features such as Ok(), NotFound() etc.. which i really want to use.
I could not find any documentation or guideline for this purpose.
Here is my code:
[HttpGet]
[Produces("applicaiton/json")]
public IActionResult GetValues()
{
var toRet = new[] {"value1", "value2"};
return Ok(toRet);
}
This codeblock returns ["value1","value2"] but i need it like this
{
"result": ["value1","value2"],
"resultCode": 200,
"resultMessage": "OK"
}
How and where can i achieve this convention?
Edit
Excuse my on my example code. I will use custom error codes in my responses. Most probably 200 OK will be
{
"result" : "some value",
"resultCode" : 0,
"resultMessage" : "All values returned"
}
And 400 Bad Request will be
{
"result" : null,
"resultCode" : 20468,
"resultMessage" : "Dont have any value to show!"
}