【发布时间】:2020-08-10 17:01:30
【问题描述】:
我尝试在 ASP.NET Core API 方法中从 ActionResult<object> 获取值。
API 有不同的控制器。我尝试在控制器 A 中使用来自控制器 B 的方法来返回结果值。我从控制器 B 获得了一个 ActionResult 对象。我可以在 ResultObject 中使用调试器查看该值,但我如何才能访问其中的结果值?
public ActionResult<object> getSourceRowCounter(string sourcePath) //Method from Controller A
{
var result = ControllerB.GetValue($"{sourcePath}.{rowCounterVariableName}");
var result2 = result.Value; //null
var result3 = result.Result; //typ: {Microsoft.AspNetCore.Mvc.OkObjectResult} <-see Value=4 in it with Debugger
//var result4 = result3.Value; //Error?!
//var result5 = result3.Content; //Error?!
//var result6 = result3.?????; //How can i get the Value = 4?
return Ok(result); //should only return value 4 and not the whole object
}
【问题讨论】:
-
这能回答你的问题吗? How to Unit Test with ActionResult<T>?
标签: c# asp.net-core asp.net-core-webapi actionresult