Created
May 1, 2018 19:24
-
-
Save Flayed/96633781be3b6a293cb8235c75d0cb0c to your computer and use it in GitHub Desktop.
Exposing Enums on Web API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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