【问题标题】:ASP.NET Core Web API with ReturnHttpNotAcceptable and custom media types results in "no output formatter found" on 4xx responses具有 ReturnHttpNotAcceptable 和自定义媒体类型的 ASP.NET Core Web API 在 4xx 响应中导致“找不到输出格式化程序”
【发布时间】:2021-04-14 09:04:15
【问题描述】:

我有一个 ASP.NET Core Web API(TargetFramework 是 net5.0),我接受 2 种媒体类型:

  • application/json
  • application/vnd.my.customtype+json

我有一个基本的控制器如下:

[ApiController]
[ApiConventionType(typeof(DefaultApiConventions))]
[Route("")]
public class RootController : ControllerBase
{
    [HttpGet]
    [HttpHead]
    [Route("", Name = "get_root")]
    [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))]
    public IActionResult Get()
    {
        return this.Ok(new { Item1 = "item 1" });
    }
}

如果我使用标题 Accept: application/json 发出 GET 请求并返回带有结果的 200 OK,一切都很好:

GET https://localhost:5001/ HTTP/1.1
Accept: application/json
Host: localhost:5001

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Kestrel
Content-Length: 18

{"item1":"item 1"}

如果我使用标头Accept: application/vnd.my.customtype+json 发出GET 请求并返回带有结果的200 OK,一切都很好:

GET https://localhost:5001/ HTTP/1.1
Accept: application/vnd.my.customtype+json
Host: localhost:5001

HTTP/1.1 200 OK
Content-Type: application/vnd.my.customtype+json; charset=utf-8
Server: Kestrel
Content-Length: 18

{"item1":"item 1"}

返回 404 的同一控制器(例如,如果找不到资源)

[ApiController]
[ApiConventionType(typeof(DefaultApiConventions))]
[Route("")]
public class RootController : ControllerBase
{
    [HttpGet]
    [HttpHead]
    [Route("", Name = "get_root")]
    [ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))]
    public IActionResult Get()
    {
        return this.NotFound();
    }
}

现在,如果我使用标头 Accept: application/json 发出 GET 请求,一切都很好(我在响应正文中得到正确的 404 和 ProblemDetails):

GET https://localhost:5001/ HTTP/1.1
Accept: application/json
Host: localhost:5001

HTTP/1.1 404 Not Found
Content-Type: application/problem+json; charset=utf-8
Server: Kestrel
Content-Length: 161

{
  "type":"https://tools.ietf.org/html/rfc7231#section-6.5.4",
  "title":"Not Found",
  "status":404,
  "traceId":"00-54d50dfe6a27674ba83a73324fcf2721-d4286734f860c94a-00"
}

如果我使用标头Accept: vnd.my.customtype+json 发出GET 请求,我会收到406 Not Acceptable 响应:

GET https://localhost:5001/ HTTP/1.1
Accept: application/vnd.my.customtype+json
Host: localhost:5001

HTTP/1.1 406 Not Acceptable
Server: Kestrel
Content-Length: 0

在日志中,我得到了这个:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET https://localhost:5001/ - -

info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Hooli.WebAPI.Controllers.RootController.Get (Hooli.WebAPI)'

info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Get", controller = "Root"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Get() on controller Hooli.WebAPI.Controllers.RootController (Hooli.WebAPI).

warn: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      No output formatter was found for content types 'application/problem+json, application/problem+xml' to write the response.

info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action Hooli.WebAPI.Controllers.RootController.Get (Hooli.WebAPI) in 0.8058ms

info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Hooli.WebAPI.Controllers.RootController.Get (Hooli.WebAPI)'

info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET https://localhost:5001/ - - - 406 0 - 1.1703ms

注意warn 行:No output formatter was found for content types 'application/problem+json, application/problem+xml' to write the response.

我不明白为什么这不会返回正确的404 和问题详细信息。

我的Startup.cs如下:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        this.Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers(
                     options =>
                     {
                         options.ReturnHttpNotAcceptable = true;
                     })
                .AddNewtonsoftJson(
                     options =>
                     {
                         options.SerializerSettings.ContractResolver =
                             new CamelCasePropertyNamesContractResolver
                             {
                                 NamingStrategy = new CamelCaseNamingStrategy(true, true)
                             };
                     });

        services.Configure<MvcOptions>(
            options =>
            {
                NewtonsoftJsonOutputFormatter newtonsoftJsonOutputFormatter =
                    options.OutputFormatters.OfType<NewtonsoftJsonOutputFormatter>().FirstOrDefault() ??
                    throw new ArgumentNullException(nameof(newtonsoftJsonOutputFormatter));

                // add media types that we want to support by default
                newtonsoftJsonOutputFormatter
                    .SupportedMediaTypes
                    .Add(MediaTypeHeaderValue.Parse("application/vnd.my.customtype+json"));
            });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseExceptionHandler(env.IsDevelopment() ? "/error-local-development" : "/error");
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
    }
}

为了完整起见,我的 Program.cs 文件:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
                   .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
    }
}

我已将我的 API 简化为这个最基本的设置,以排除任何额外的中间件/配置。我返回什么状态码并不重要,如果它是任何4xx 响应,当Accept 标头是自定义媒体类型时,我总是会得到406

例如

  • this.BadRequest();
  • this.NotFound();
  • this.Unauthorized();

如果我将options.ReturnHttpNotAcceptable = true 更改为options.ReturnHttpNotAcceptable = false,所有响应都可以正常返回,但是当Accept 标头中传递了无效的媒体类型时,API 不会返回 406。

当自定义媒体类型位于 Accept 标头中时,如何让 API 返回正确的响应?

【问题讨论】:

  • 你需要实现你自己的自定义IOutputFormatter并配置你的mvc中间件来使用它,看看这里docs.microsoft.com/en-us/aspnet/core/web-api/advanced/…这里自定义的媒体类型是application/problem+json
  • @KingKing,有什么方法可以告诉NewtonsoftJsonOutputFormatter 来处理它吗?最终,输出将与application/json 相同,NewtonsoftJsonOutputFormatter 已经处理。
  • 我不太确定,但是您可以有一个非常简单的 IOutputFormatter 实现,因为它使用 System.Text.Json 或其他一些 JSON 库。我不知道你为什么想要这样一个自定义媒体类型(但实际上只是标准类型application/json 的别名),如果你尝试尽快将Accept 标头值更改为application/json 怎么办?服务器端?也许输出处理会考虑最新的标头值。
  • 我看到您的代码中有这个newtonsoftJsonOutputFormatter.SupportedMediaTypes,您是否尝试将媒体类型application/problem+json 添加到那里? CustomMediaTypeNames.Global.HateoasJson 的值是多少?因为那已经添加了。
  • @KingKing,抱歉,这是复制/粘贴的剩余部分;我已经更新了该代码。实际值是newtonsoftJsonOutputFormatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/vnd.my.customtype+json")); 我也添加了newtonsoftJsonOutputFormatter.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/problem+json"));,但我仍然得到406(如上)

标签: asp.net asp.net-core asp.net-core-webapi media-type


【解决方案1】:

试试这个:

添加一个类:

public class CustomOutputFormatter: OutputFormatter
    {
        public CustomOutputFormatter()
        {
            SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/vnd.my.customtype+json"));
        }
        
        public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
        {
            var response = context.HttpContext.Response;
            await response.WriteAsJsonAsync(context.Object);
        }
    }

并在您的服务中注册:

services
    .AddControllers(options =>
    {
        options.RespectBrowserAcceptHeader = true;
        options.ReturnHttpNotAcceptable = true;
    })
    .AddMvcOptions(o => o.OutputFormatters.Add(new CustomOutputFormatter()))

【讨论】:

  • 这适用于成功的响应,但是当任何 4xx 响应发生时,它仍然以 406 的形式返回。warn: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]No output formatter was found for content types 'application/problem+json, application/problem+xml' to write the response.
  • 不确定是什么问题。我在SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("vnd.my.customtype+json")); 中有错字,应该是application/vnd.my.customtype+json,你纠正了这部分吗?你能发布你的新ConfigureServices() 方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 2017-08-19
  • 2020-01-21
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
相关资源
最近更新 更多