Примерно так:
public class APIResult
{
public int errorCode { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string errorMessage { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object messageData { get; set; }
public APIResult(int errorCode, string errorMessage, object data)
{
this.errorCode = errorCode;
this.errorMessage = errorMessage;
messageData = data;
}
}
public abstract class BaseAPIController : Controller
{
protected JsonResult ApiMessage(int errorCode, string errorMessage = null, object data = null)
{
return Json(new APIResult(errorCode, errorMessage, data));
}
}
// И пример использования
public YourController: BaseAPIController
{
public async Task<IActionResult> YourAction(int id)
{
return ApiMessage(0, data: new { id = id });
// Или
// return ApiMessage(-2, errorMessage: "Invalid request parameters");
}
}