Create Request and Response Dto - bondaigames/ASP.NET-Core-Documentation GitHub Wiki

  • Create New Web MVC Project

  • Create ResponseDto.cs

using Utility;
using System.Text.Json.Serialization;

namespace Web.Admin.Models.Dto
{
    public class ResponseDto
    {
        /*
         -- Successful request --
         {
            "data": {
                "id": 1001,
                "name": "Wing"
            }
         }

         -- Failed request: --
          {
            "error": {
                "code": 404,
                "message": "ID not found"
            }
          }
        */

        [JsonPropertyName(PN.Data)]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault | JsonIgnoreCondition.WhenWritingNull)]
        public object? Result { get; set; }
        [JsonPropertyName(PN.Success)]
        public bool IsSuccess { get; set; } = true;
        [JsonPropertyName(PN.Message)]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault | JsonIgnoreCondition.WhenWritingNull)]
        public string? Message { get; set; }
    }
}

  • Create SD.cs in Utility
namespace Web.Admin.Utility
{
//Static Detail
    public class SD
    {
        public enum ApiType
        {
            GET, 
            POST, 
            PUT, 
            DELETE
        }
    }
}

  • Create RequestDto in Models\Dto folder
using static Web.Admin.Utility.SD;

namespace Web.Admin.Models.Dto
{
    public class RequestDto
    {
        public ApiType ApiType { get; set; } = ApiType.GET;
        public string Url { get; set; }
        public object Data { get; set; }
        public string AccessToken { get; set; }
    }
}