How to return an enum ID instead of the enum text in WebAPI

Posted by Rodney on Stack Overflow See other posts from Stack Overflow or by Rodney
Published on 2013-06-26T02:34:18Z Indexed on 2013/06/26 22:21 UTC
Read the original article Hit count: 167

Filed under:
|

I am using the WebAPI with .NET 4.5. I have a enum called DareStatus which is a list of statuses and their corresponding integer ID's. To minimize bandwidth traffic I want to send the int values of the enums, not the full text as it is currently doing. (I have control over the client so I can map it on the clientside). The strange thing is that in my RTFM-ing everyone seems to have the opposite issue!

http://www.ftter.com/desktopmodules/framework/api/dare/getalldares

public enum DareStatus : int
{
    All = 0,
    Accepted = 2,
    Pending = 1,
    Declined = 3,
    Cancelled = 4,
    Failed = 5,
    Won = 6
}


    public IEnumerable<DareInfo> GetDares()
    {
        IEnumerable<DareInfo> dareInfos;
        using (IDataContext ctx = DataContext.Instance())
        {
            var rep = ctx.GetRepository<DareInfo>();
            dareInfos = rep.Get();

        }
        return dareInfos;
    }

© Stack Overflow or respective owner

Related posts about enums

Related posts about asp.net-web-api