Support for Asp.Net Webapi OData V4 Enum Key

I have implemented ASP.Net Webapi (V4.6) with support for OData V4. My controllers inherit from ODataController, and I use the standard Json Serializer.

Some of my objects have enumeration fields, and I want to serialize enumeration keys, not the corresponding value. I am using OData with Asp.Net.WebApi.OData v5.7 and cannot find a suitable solution. Does anyone have an idea to implement this?

Entity Class:

public class MyEntity
{
    #region Properties     

    public int Id { get;set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    public ProjectStatus Status { get; set; }
    public double EstimatedCost { get; set; }

    #endregion
}

public enum ProjectStatus
{
    New = 10,
    Active = 20,
    Paused = 30,
    Finished = 40,
}

Result after serialization: {"Id": 1, "StartDate": zero, "EndDate": zero, "Status": "Active" , "EstimatedCost": 0,0}

( "" : "20" ) , , : "" : "".

+4

Source: https://habr.com/ru/post/1627032/


All Articles