Skip to content

Add option to suppress charset in Content-Type for JSON responses #66838

@Dave-Senn

Description

@Dave-Senn

Is there an existing issue for this?

  • I have searched the existing issues

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions