首页 > 解决方案 > 如何将字符串列表视为模型中的枚举 - ASP.net Core Web API

问题描述

我有一个 ASP.net Core Web API (v3.1),它接受一个具有枚举属性的模型。示例类和枚举:

public class OrderModel 
{
    public int OrderNumber { get; set; }
    public OrderType OrderType { get;set; }
    ...
}

public enum OrderType 
{
    Offline = 0, 
    Checkout = 1
}

然后在大摇大摆中显示如下:

在此处输入图像描述

我想定义一个字符串列表,例如:

public class OrderTypes 
{
   public List<string> GetOrderTypes() 
   {
        return new List<string> { "Offline", "Checkout" }; 
        // in reality this will be dynamically built at startup.
   }
}

我想做的是将我的模型更改为与此类似:

public class OrderModel 
{
    public int OrderNumber { get; set; }
    public string OrderType { get;set; }
    ...
}

但是以与枚举类似的方式显示 OrderType 的选项。

这可能吗?

注意:我的实际用例是反映(在运行时)特定接口的所有实现。这将是一个不断增长的列表,并且会随着时间的推移而增长——我不想在这些提供商增长时维护它们的枚举列表。

标签: c#asp.net-coreasp.net-web-apiswaggerswashbuckle

解决方案


推荐阅读