我有 Controller 和一些端点 Task<IActionResult> MyCustomEndpoint
这是返回 return Ok(CustomDataType)
.返回的数据是来自 JSON 的格式。
现在我想从其他 Controller 调用该端点,例如 var resp = myController.MyCustomEndpoint
,其中 resp 变为 IActionResult
.问题是 resp
现在不再只返回数据,而是返回图像上看到的所有这些字段。
我的问题是,如何只访问和返回Value
字段,因为 resp.Value
不工作。
感谢您的帮助。
最佳答案
你必须转换为 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);
}
关于asp.net - 从 IActionResult 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037038/