- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试测试返回 IActionResult
的 API Controller 的结果。目前它正在返回一个包含状态代码、值等的对象。我正在尝试仅访问该值。
List<Batch.Context.Models.Batch> newBatch2 = new List<Batch.Context.Models.Batch>();
var actionResultTask = controller.Get();
actionResultTask.Wait();
newBatch2 = actionResultTask.Result as List<Batch.Context.Models.Batch>;
actionResultTask.Result
返回一个列表,其中包含一个列表“Value”,该列表是 Batch.Context.Models.Batch
的列表,我无法访问该值。将其转换为列表后,它变为 null。
这是 Controller
[HttpGet]
[ProducesResponseType(404)]
[ProducesResponseType(200, Type = typeof(IEnumerable<Batch.Context.Models.Batch>))]
[Route("Batches")]
public async Task<IActionResult> Get()
{
var myTask = Task.Run(() => utility.GetAllBatches());
List<Context.Models.Batch> result = await myTask;
return Ok(result);
}
如何访问列表中的值。
最佳答案
那是因为Task
的Result
是一个IActionResult
派生类,OkObjectResult
使测试异步。等待被测方法。然后执行所需的断言。
例如
public async Task MyTest {
//Arrange
//...assume controller and dependencies defined.
//Act
IActionResult actionResult = await controller.Get();
//Assert
var okResult = actionResult as OkObjectResult;
Assert.IsNotNull(okResult);
var newBatch = okResult.Value as List<Batch.Context.Models.Batch>;
Assert.IsNotNull(newBatch);
//...other assertions.
}
关于c# - 如何从 IActionResult 中提取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50821691/
在 Controller 中在 .NET Core 中你可以返回 Ok()作为IActionResult .但我不明白它怎么还可以返回一个Task . 例子: public class Foo
Razor Pages 在 Asp.Net Core 2.0 中引入,作为创建以页面为中心的 Web 应用程序的一种方式,并且在许多方面比典型的 Controller 、 View 、模型方法更容易编
我们将在 dotnet core 2.1 中创建一个新的 API,这个 Web API 将是一个高流量/交易,比如每分钟 10,000 或更高。我通常像下面这样创建我的 API。 [HttpGet("
虽然我确实理解 Task、ActionResult 等概念。但我仍然不确定如果没有指定其他内容,在 Controller 中输入哪个最直观。 考虑到返回类型的明确性,我应该这样做: [HttpGet]
我正在开发 .Net Core Web API 应用程序。我的 Controller 上有以下方法: [HttpGet] public async Task GetArtists() { var
我刚刚下载了基于 ASP.NET 5 的 music store (microsoft sample projct) 源代码。我不明白为什么 Microsoft 的开发人员在 Controller 中
我想设置一个全局设置,以便在从任何 HTTP 函数返回的任何响应中不返回 null 属性。 示例: public class User { public int Id { get; set;
我正在使用 .NET Core 2 创建一个 API,为使用不同技术开发的许多应用程序提供数据。我目前正在从我的方法返回 IActionresult。我一直在研究返回数据的最佳选择是什么,并看到了一些
我正在将以下请求发布到 http://somesite.ngrok.io/nexmo?to=61295543680&from=68889004478&conversation_uuid=CON-btt
我正在使用 barcodelib 库 + .Net Core 中可用的 System.Drawing.Common 包生成条码图像。 我想在浏览器中将图像作为纯图像(或作为下载)返回给用户,但我似乎找
我正在尝试测试返回 IActionResult 的 API Controller 的结果。目前它正在返回一个包含状态代码、值等的对象。我正在尝试仅访问该值。 List newBatch2 = new
我正在尝试使用 IActionResult 接口(interface)来抛出特定的异常。但是此刻我被困在 Controller 上了。我编写了以下代码来获得单个调用: [Route("sing
我正在使用 C# 和 .NET Core 2.0 开发 ASP.NET Core 2 Web API。 我已经更改了一种方法,将其添加到 try-catch 中以允许我返回状态代码。 public I
我有 Controller 和一些端点 Task MyCustomEndpoint这是返回 return Ok(CustomDataType) .返回的数据是来自 JSON 的格式。 现在我想从其他
我有一个端点,它调用另一个返回 json 对象 (IActionResult) 的函数。如何访问 json 数据? public async Task GetData( [Ht
我已经创建了 Web API,但我的问题是从它读取结果给客户端。 创建用户的WebApi方法: [HttpPost] public IActionResult PostNewUser([FromBod
我已经开始研究 Typewriter,看看它是否符合我生成模型和 API 层的要求。 到目前为止,它正在生成模型,我让它生成某种 API 层,但是在使用 $ReturnType 时我遇到了问题,如示例
什么是 IActionResult?我尝试查看 MSDN 和其他站点,但需要一般的、常见的、易于理解的答案。 MSDN IActionResult 示例: public IActionResult A
我有一个 API Controller 端点,例如: public IHttpActionResult AddItem([FromUri] string name) { try {
我有一个只有一个 Action 的 Controller 。在这个 Action 方法中,我有一个 async我调用的方法就是这样。这是我正在使用的代码: [HttpGet] public Task
我是一名优秀的程序员,十分优秀!