【问题标题】:Get value from IActionResult从 IActionResult 获取值
【发布时间】:2018-08-27 10:11:13
【问题描述】:

我有一些端点Task<IActionResult> MyCustomEndpoint 的控制器返回return Ok(CustomDataType)。返回的数据是 JSON fromat。

现在我想从其他控制器(如var resp = myController.MyCustomEndpoint)调用该端点,其中相应变为IActionResult。问题是resp 现在不再只返回数据,而是所有这些字段,如图所示。

我的问题是,如何只访问和返回Value 字段,因为resp.Value 不起作用。

感谢您的帮助。

【问题讨论】:

标签: asp.net asp.net-mvc .net-core asp.net-core-mvc


【解决方案1】:

您必须转换为as ObjectResult

public async Task GetFormSummary_ItShouldReturnSingleForm_NotNull()
{

    var formId = new Guid("5EF685E7-1167-4226-7F5E-08D9009544A3");
   
    var mockFormService = new Mock<IFormService>();

    mockFormService.Setup(x =>  x.GetAsync(formId).Result)
        .Returns(GetTestForm());

    // Inject
    var formController = new FormController(_logger, mockFormService.Object);

    // Act
    var result = (await formController.GetAsync(formId)) as ObjectResult;

    // Assert
     Assert.IsAssignableFrom<FormViewModel>(result.Value);

}

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 2021-06-04
    • 1970-01-01
    • 2019-07-30
    相关资源
    最近更新 更多