简体   繁体   English

从 ActionResult 中获取值<object>在 ASP.Net Core API 方法中<div id="text_translate"><p>我尝试从 ASP.NET 核心 API 方法中的ActionResult&lt;object&gt;获取值。</p><p> API 有不同的 controller。 我尝试使用 controller A 中的 controller B 中的方法返回结果值。 我从 controller B 得到一个ActionResult object。我可以在ResultObject中使用调试器查看该值,但是如何访问其中的结果值?</p><pre> public ActionResult&lt;object&gt; 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} &lt;-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 }</pre> <p><img src="https://i.stack.imgur.com/4Lppi.jpg" alt="在此处输入图像描述"></p></div></object>

[英]Get a Value from ActionResult<object> in a ASP.Net Core API Method

I try to get a value from ActionResult<object> in an ASP.NET Core API method.我尝试从 ASP.NET 核心 API 方法中的ActionResult<object>获取值。

The API has a different controller. API 有不同的 controller。 I try to use a method from controller B in controller A to return the result value.我尝试使用 controller A 中的 controller B 中的方法返回结果值。 I get an ActionResult object from controller B. I can see the value with the debugger in the ResultObject but how can I get access to the result value in it?我从 controller B 得到一个ActionResult object。我可以在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
}

在此处输入图像描述

If you're sure that it is a type of OkObjectResult then cast before using it like below:如果您确定它是OkObjectResult的一种类型,请在使用它之前进行转换,如下所示:

var result3 = (OkObjectResult)result.Result; // <-- Cast is before using it.
var result4 = result3.Value; //<-- Then you'll get no error here.

If you are calling an API from action and the API returns Ok(result) which you want to access in your action then below code can help you:如果您从操作中调用 API 并且 API 返回您想要在操作中访问的 Ok(result),那么下面的代码可以帮助您:

if (response.StatusCode == System.Net.HttpStatusCode.OK)
{                                   
    return Ok(response.Content); // returns API result
}
Suppose you have example function in Repository.cs :假设您在Repository.cs中有示例 function :
public async Task<IEnumerable<Todo>> GetTodosAsync() =>
   await _context.Todos.ToListAsync();
And function in Controller.cs looks like below: Controller.cs 中的Controller.cs如下所示:
public async Task<ActionResult<IEnumerable<Todo>>> GetTodosAsync() =>
    Ok(await _repository.GetTodosAsync());
Then in your UnitTest.cs you can get results by doing this:然后在您的UnitTest.cs中,您可以通过执行以下操作获得结果:
var result = await controller.GetCustomersAsync();
// list of todos is in `actual` variable
var actual = (result.Result as OkObjectResult).Value as IEnumerable<CustomerPayload>;
// or use `ObjectResult` instead of `OkObjectResult` for multiple types

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core中如何获取ActionResult StatusCode - How to get ActionResult StatusCode in ASP.NET Core 单元测试中的 ActionResult 问题无法在 POST 返回值上 Assert.Equal (asp.net core mvc web api) - Trouble with ActionResult in unit test can't Assert.Equal on POST return value (asp.net core mvc web api) .NET Core Web API - 如何从 ActionResult 获取响应信息 - .NET Core Web API - How to Get Response Information from ActionResult Asp.net 5 web api 自定义操作结果 - Asp.net 5 web api Custom ActionResult 将带有 JSON 数据的 GET 请求从 Axios 发送到 Asp.net 核心 ActionResult 而不获取(415 不支持的媒体类型) - Send GET request with JSON data from Axios to Asp.net core ActionResult without getting (415 Unsupported Media Type) 如何将数组从Angular 6传递到ASP.NET Core API的GET方法? - How do I pass array from Angular 6 to ASP.NET Core API for GET method? Controller方法返回ActionResult的原因是什么? (ASP.NET Core Web API) - What's the reason why Controller methods return ActionResult? (ASP.NET Core Web API) ASP.NET Core API - ActionResult<T> vs 异步任务<T> - ASP.NET Core API - ActionResult<T> vs async Task<T> ASP.net 核心 API 返回 ActionResult<t> 不强制返回类型</t> - ASP.net Core API returning ActionResult<T> not enforcing return type 为什么 ASP.NET Core 操作可以返回 `ActionResult <IEnumerable<ZapResult> &gt;` 返回任何对象的 `BadRequest()`? - Why can an ASP.NET Core action returning an ` ActionResult<IEnumerable<ZapResult>>` return a `BadRequest()` of any object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM