英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论