asp.net-mvc - JsonResult 和 ObjectResult 不是相互派生的——为什么?
问题描述
我团队中的人写的东西是这样的:
[HttpPost("do")]
public async Task<ActionResult> DoAsync()
{
try
{
var obj = await this.DoThing();
return new JsonResult(obj);
}
catch (StatusCodeException x)
{
return StatusCode(x.StatusCode, new { x.Message, x.StackTrace });
}
catch (Exception x)
{
return StatusCode(500, x.GenerateMessage(" "));
}
}
这意味着JsonResult
或ObjectResult
可以返回,这使测试变得复杂。这是因为JsonResult
和ObjectResult
不相互派生。有谁知道为什么会这样?
我必须编写一个小包装类进行测试,以使生活更轻松:
/// <summary>
/// Defines all subclasses of <see cref="ActionResult"/>
/// that have status codes and <see cref="object"/> values.
/// </summary>
public class ObjectValueActionResult
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectValueActionResult"/> class.
/// </summary>
/// <param name="result">The result.</param>
/// <remarks>
/// The majority of <see cref="IActionResult"/> instances like <see cref="OkObjectResult"/> derive
/// from <see cref="ObjectResult"/>.
/// </remarks>
public ObjectValueActionResult(IActionResult result)
{
Assert.NotNull(result);
switch (result)
{
case JsonResult j:
this.Value = j.Value;
this.StatusCode = j.StatusCode;
break;
case NoContentResult n:
this.StatusCode = n.StatusCode;
break;
case ObjectResult o:
this.Value = o.Value;
this.StatusCode = o.StatusCode;
break;
default:
throw new NotImplementedException($"The expected {nameof(IAsyncResult)} type is not here.");
}
}
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>
/// The value.
/// </value>
public object Value { get; set; }
/// <summary>Gets or sets the HTTP status code.</summary>
public int? StatusCode { get; set; }
}
有没有替代方案?
解决方案
JsonResult
旨在返回 JSON 格式的数据,无论通过 Accept 标头请求什么格式,它都会返回 JSON。当我们使用 JsonResult 时,不会发生内容协商。
内容协商是确定浏览器通过其 Http 请求 Accept 标头请求的数据类型的过程。例如,这是一个请求 HTML: 类型内容的接受标头,Accept: application/xml, */*; q=0.01
操作结果为 JsonResult 类型,不会发生内容协商。这意味着服务器会忽略用户请求的类型并返回 JSON 。
ObjectResult
是一个内置内容协商的 IActionResult。通常,除非您指定 Accept 标头,否则 API 会将响应序列化为 JSON。如果您指定例如 'application/xml' ,它将返回 XML 。
参考 :
http://hamidmosalla.com/2017/03/29/asp-net-core-action-results-explained/
https://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.2
推荐阅读
- matrix - cublasSgemm 行主要乘法
- azure - 未为 Azure 上的身份服务器 4 生成刷新令牌。只获取授权码
- node.js - 当我尝试在 cPanel 上安装 Laravel 时出现 pngquant 错误
- c# - Visual Studio 2017 - Intellisense 不显示文档
- javascript - XMLHttpRequest 与 Django -> GET,错误 500
- git - .gitignore 中的文件夹仍在回购中
- css - Bootstrap 4 容器宽度不正确
- sql - 将具有时间长度的字符串转换为不同的数据类型
- docker - 容器 Docker 运行后退出
- html - ie11 在输入文本字段中按 [Enter] 会导致文本换行