Skip to content

Instantly share code, notes, and snippets.

@Flayed
Created May 1, 2018 19:24
Show Gist options
  • Save Flayed/96633781be3b6a293cb8235c75d0cb0c to your computer and use it in GitHub Desktop.
Save Flayed/96633781be3b6a293cb8235c75d0cb0c to your computer and use it in GitHub Desktop.
Exposing Enums on Web API
public enum Day
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
public enum Month
{
January = 1,
February = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
[HttpGet("Days")]
public IActionResult GetDays()
{
return Ok(GetEnum(typeof(Day)));
}
[HttpGet("Months")]
public IActionResult GetMonths()
{
return Ok(GetEnum(typeof(Month)));
}
private IEnumerable<(string key, int value)> GetEnum(Type enumType)
{
return Enum.GetValues(enumType)
.Cast<int>()
.Select(e => (Enum.GetName(enumType, e), e));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment