Код: Выделить всё
public static class GetPlayer
{
public sealed class Response
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Displayed { get; set; }
public PositionName Position { get; set; }
public ICollection Heroes { get; set; }
public DateTime UpdatedAt { get; set; }
}
Код: Выделить всё
public static class GetHero
{
public sealed class Response
{
public int Id { get; set; }
public string Name { get; set; }
public MainStat MainStat { get; set; }
}
Код: Выделить всё
public static class GetPosition
{
public sealed class Response
{
public int Id { get; set; }
public PositionName Name { get; set; }
}
Код: Выделить всё
public enum MainStat
{
Agility,
Intelligence,
Strength,
Universal
}
public enum PositionName
{
Carry = 1,
Midlane = 2,
Offlane = 3,
Roamer = 4,
Support = 5,
}
Украшение свойства класса -
https:// Exceptionnotfound.net/serializing-enumerations-in-asp-net-web-api/
Украсьте перечисление -
https://Exceptionnotfound.net/serializi ... ns-in-asp- net-web-api/
Добавить конвертер глобально -
https://Exceptionnotfound.net/serializi ... t-web-api/
За исключением того, что я использовал JsonStringEnumConverter из System.Text.Json.
Глобальное добавление конвертера ничего не дает:
Код: Выделить всё
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
Код: Выделить всё
{
"id": "4e82ced0-6573-49ee-9e73-92ad97051a21",
"userId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "test",
"description": "test",
"displayed": false,
"position": 0,
"heroes": [
{
"id": 1,
"name": "Juggernaut",
"mainStat": "Agility"
},
{
"id": 3,
"name": "Crystal Maiden",
"mainStat": "Intelligence"
}
],
"updatedAt": "2024-07-24T16:39:12.244029Z"
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... to-strings
Мобильная версия