Postman 返回有意义的.Net Core中的完整异步操作结果

Postman 返回有意义的.Net Core中的完整异步操作结果,postman,asp.net-core-3.1,Postman,Asp.net Core 3.1,当找不到资源时,我的控制器返回一个文本字符串 代码 [HttpGet("{productId}/icon", Name = "GetProductIcon")] [Produces("application/json")] public async Task<ActionResult<ProductIconDto>> GetIconForProduct(int productId) {

当找不到资源时,我的控制器返回一个文本字符串

代码

[HttpGet("{productId}/icon", Name = "GetProductIcon")]
    [Produces("application/json")]
    public async Task<ActionResult<ProductIconDto>> GetIconForProduct(int productId)
    {
        try
        {
            return await Mediator.Send(new GetProductIconQuery {Id = productId});
        }
        catch(NotFoundException ex)
        {
            return NotFound(ex.Message);
        }
    }
[HttpGet("{productId}/icon", Name = "GetProductIcon")]
    [Produces("application/json")]
    public async Task<ActionResult<ProductIconDto>> GetIconForProduct(int productId)
    {
        try
        {
            return await Mediator.Send(new GetProductIconQuery {Id = productId});
        }
        catch(NotFoundException ex)
        {
            return NotFound(new JsonResult(ex.Message));
        }
    }
[HttpGet(“{productId}/icon”,Name=“GetProductIcon”)]
[产生(“应用程序/json”)]
公共异步任务

当我添加一个Json解析器时,我就更接近我想要的行为了

代码

[HttpGet("{productId}/icon", Name = "GetProductIcon")]
    [Produces("application/json")]
    public async Task<ActionResult<ProductIconDto>> GetIconForProduct(int productId)
    {
        try
        {
            return await Mediator.Send(new GetProductIconQuery {Id = productId});
        }
        catch(NotFoundException ex)
        {
            return NotFound(ex.Message);
        }
    }
[HttpGet("{productId}/icon", Name = "GetProductIcon")]
    [Produces("application/json")]
    public async Task<ActionResult<ProductIconDto>> GetIconForProduct(int productId)
    {
        try
        {
            return await Mediator.Send(new GetProductIconQuery {Id = productId});
        }
        catch(NotFoundException ex)
        {
            return NotFound(new JsonResult(ex.Message));
        }
    }
[HttpGet(“{productId}/icon”,Name=“GetProductIcon”)]
[产生(“应用程序/json”)]
公共异步任务

我实际想要的是返回NotFound结果,并提供一个已填写的内容类型、状态代码和消息。理想情况下,我希望在Startup.cs中配置这个类,或者创建一个可以在任何地方使用的类。

使用
Content()
ContentResult
并在返回结果之前将
StatusCode
设置为404