Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Some external specifications (especially in payment integrations) require responses to use exactly: Content-Type: application/json and explicitly reject: Content-Type: application/json; charset=utf-8
At the moment, ASP.NET Core automatically appends the charset parameter for JSON responses, and there does not appear to be a built-in way to suppress it globally or per endpoint.
My current workaround is to manually rewrite the response header using middleware:
public sealed class OverwriteContentTypeMiddleware( RequestDelegate next )
{
public async Task InvokeAsync( HttpContext context )
{
context.Response.OnStarting( _ =>
{
if ( String.IsNullOrEmpty( context.Response.Headers.ContentType ) )
return Task.FromResult( 0 );
context.Response.Headers.Remove( "Content-Type" );
context.Response.Headers.Append( "Content-Type", "application/json" );
return Task.FromResult( 0 );
},
context );
await next.Invoke( context );
}
}
This works, but it's not a great solution.
Describe the solution you'd like
Suggested solution
It would be helpful to have an official configuration option to suppress the charset parameter for JSON responses.
For example, something along the lines of:
builder.Services.AddJsonOptions( options =>
{
options.SuppressCharsetInContentType = true;
} )
While RFCs generally treat UTF-8 JSON as the default, some third-party systems and payment specifications validate the Content-Type header strictly and reject requests/responses that include additional parameters.
Having a built-in option would avoid custom middleware and make these integrations cleaner and less error-prone.
Additional context
No response
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
Some external specifications (especially in payment integrations) require responses to use exactly:
Content-Type: application/jsonand explicitly reject:Content-Type: application/json; charset=utf-8At the moment, ASP.NET Core automatically appends the charset parameter for JSON responses, and there does not appear to be a built-in way to suppress it globally or per endpoint.
My current workaround is to manually rewrite the response header using middleware:
This works, but it's not a great solution.
Describe the solution you'd like
Suggested solution
It would be helpful to have an official configuration option to suppress the charset parameter for JSON responses.
For example, something along the lines of:
While RFCs generally treat UTF-8 JSON as the default, some third-party systems and payment specifications validate the Content-Type header strictly and reject requests/responses that include additional parameters.
Having a built-in option would avoid custom middleware and make these integrations cleaner and less error-prone.
Additional context
No response