HttpContext.Response.ContentType is set by default as "application/json; charset=utf-8" – how to change this default?

huangapple go评论68阅读模式
英文:

HttpContext.Response.ContentType is set by default as "application/json; charset=utf-8" - how to change this default?

问题

I'm having an ASP.NET Core 6.0 Web API system where I'm caching requests to replay them (an idempotency layer). On the response pipeline, I get a Content-Type header with the value "application/json; charset=utf-8". But I see this question link that says that this header value is not right.

Then, in practice I see that it actually is giving me problems because when I replay the response with this header value, the SystemTextJsonOutputFormatter is not found as a valid output formatter (It matches just "application-json" and not "application-json; charset=utf-8").

Checking the source I see that SystemTextJsonOutputFormatter has SupportedEncodings.Add(Encoding.UTF8); as well as SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson);

The only workaround I've found is to explicitly add this media type to the formatter like this:

builder.Services.AddControllers(options => {
    var formatter = (TextOutputFormatter)options.OutputFormatters.
                    Where(o => o.GetType() == typeof(SystemTextJsonOutputFormatter)).First();
    formatter.SupportedMediaTypes.Add("application/json; charset=utf-8");
});

It's not working around the right problem, in my opinion.

How can I set the response default not to contain the charset?

英文:

I'm having an ASP.NET Core 6.0 Web API system where I'm caching requests to replay them (an idempotency layer). On the response pipeline, I get a Content-Type header with the value "application/json; charset=utf-8". But I see this question https://stackoverflow.com/questions/19814908/what-content-type-header-to-use-for-json-application-json-charset-utf-8-or that says that this header value is not right.

Then, in practice I see that it actually is giving me problems, because when I replay the response with this header value that SystemTextJsonOutputFormatter is not found as a valid output formatter (It matches just "application-json" and not "application-json; charset=utf-8").

Checking the source I see that SystemTextJsonOutputFormatter has SupportedEncodings.Add(Encoding.UTF8); as well as SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson);

The only workaround I've found is to explicitly add this media type to the formatter like this:

builder.Services.AddControllers(options => {
    var formatter = (TextOutputFormatter)options.OutputFormatters.
                    Where(o => o.GetType() == typeof(SystemTextJsonOutputFormatter)).First();
    formatter.SupportedMediaTypes.Add("application/json; charset=utf-8");
});

It's not working around the right problem, in my opinion.

How can I set the response default not to contain the charset?

答案1

得分: 1

一种更改响应的 Content-Type 的方法是使用类似这样的 Produces 属性:

[Produces("application/json")]

这是我迄今为止找到的最干净的选项。

英文:

One way to change the response's Content-Type is using the Produces attribute like this:

[Produces("application/json")]

This is the cleanest option I've found so far.

huangapple
  • 本文由 发表于 2023年5月6日 15:25:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定