Created
October 6, 2025 09:30
-
-
Save uzbekdev1/be58b172983e8538e127fd8cc4e480d6 to your computer and use it in GitHub Desktop.
api base response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ApiResponse<T> | |
| { | |
| public ApiResponse() | |
| { | |
| Success = true; | |
| } | |
| public ApiResponse(string error) | |
| { | |
| Error = error; | |
| Success = false; | |
| } | |
| public ApiResponse(T data) | |
| { | |
| Data = data; | |
| Success = true; | |
| } | |
| [JsonProperty("success")] | |
| public bool Success { get; set; } | |
| [JsonProperty("data")] | |
| public T Data { get; set; } | |
| [JsonProperty("error")] | |
| public string Error { get; set; } | |
| [JsonProperty("code")] | |
| public int Code { get; set; } | |
| public override string ToString() | |
| { | |
| return JsonConvert.SerializeObject(this); | |
| } | |
| } | |
| public class ApiResponse : ApiResponse<object> | |
| { | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ApiController] | |
| [Route("api/[controller]/[action]")] | |
| [Produces("application/json")] | |
| public abstract class BaseController : ControllerBase | |
| { | |
| [ProducesDefaultResponseType(typeof(ApiResponse))] | |
| protected new IActionResult Ok(object result = null) | |
| { | |
| var model = new ApiResponse() | |
| { | |
| Data = result, | |
| Error = null, | |
| Success = true, | |
| Code = StatusCodes.Status200OK | |
| }; | |
| return base.Ok(model); | |
| } | |
| [ProducesDefaultResponseType(typeof(ApiResponse))] | |
| protected IActionResult BadRequest(string error = null) | |
| { | |
| throw new Exception(error); | |
| } | |
| protected IActionResult Problem(string error = null) | |
| { | |
| return base.Problem(error); | |
| } | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment