【问题标题】:C# HttpGet Enum.GetValues return listeC# HttpGet Enum.GetValues 返回列表
【发布时间】:2021-12-07 08:39:48
【问题描述】:

我想返回“Liste”,但不知道如何返回。

[HttpGet("GetType")]
public async Task<IActionResult> GetTemplateType()
{
    var liste = Enum.GetValues(typeof(NotificationTemplateType));
    return liste;
}

我收到此错误:

【问题讨论】:

标签: c#


【解决方案1】:

虽然注释提示没有错,但您在这里有一个演练版,首先使用 xUnit 作为扩展版

public enum NotificationTemplateType
{
    Undefined = 0,
    Pigeon = 1,
    Shout = 2,
    Mail = 3,
    Team = 4,
    Skype = 5,
    Slack = 6,
    Viber = 7,
}

[Fact]
public void VerifyIsAListTest()
{
    var undefinedArray = Enum.GetValues(typeof(NotificationTemplateType));
    var specificArray = (NotificationTemplateType[])undefinedArray;
    var elementsListThoughArrayWillBeFineWhenReturningBesidesForTheController = specificArray.ToList();
    Assert.IsType<List<NotificationTemplateType>>(elementsListThoughArrayWillBeFineWhenReturningBesidesForTheController);
}

所以你的方法可能是(请用换行符更好地格式化)

 [HttpGet("GetType")]
 public async Task<IActionResult> GetTemplateType()
 {
    var liste = ((NotificationTemplateType[])Enum.GetValues(typeof(NotificationTemplateType))).ToList();
    return Ok(liste);
 }

【讨论】:

  • 因此,如果您对控制器进行单元测试并检查返回的回复正文是否包含一个列表,那么当您运行实例化控制器时,它在技术上会失败,因为它具有类型感知返回类型并且数组不是列表,但生成的 json 是相同的,因此如果您不执行 TDD,则可以直接返回数组的 cmets 为 true,否则按照所述进行转换
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 2023-03-18
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
相关资源
最近更新 更多