【问题标题】:HTTP GET Return List Enumeration .Net CoreHTTP GET 返回列表枚举 .Net Core
【发布时间】:2021-12-07 03:00:39
【问题描述】:

我有一个enum 和其中的值。我想用get方法获取这些值的列表。我刚开始,不知道怎么做。

public enum NotificationTemplateType
{
    [Description("Şablonsuz")] NotTemplate = 0,

    // User Şablonları - 100
    [Description("Yeni Kullanıcı Şablonu")] NewUser = 100,
    [Description("Şifremi Unuttum Şablonu")] ForgotPassword = 101,

    // Seller Şablonları - 200 
    [Description("Yeni Satıcı Şablonu")] NewSeller = 200,
}

[HttpGet("GetType")]
    public List<IActionResult> GetType(NotificationTemplateType notificationTemplateType)
    {
        return Ok(notificationTemplateType.GetAttributeOfType<DescriptionAttribute>()?.Description);
    }

【问题讨论】:

标签: c# enums http-get


【解决方案1】:

你可以通过GetValues获取一个枚举的所有值:

Enum.GetValues(typeof(NotificationTemplateType))

由于这会返回一个数组并且您需要一个列表,因此您只需调用ToList 方法:

Enum.GetValues(typeof(NotificationTemplateType)).ToList()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2015-11-28
    • 1970-01-01
    • 2019-08-28
    相关资源
    最近更新 更多