Skip to content

ASP.NET 6 JSON special characters serialization issue #42833

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


Hi, I'm working on Blazor Server project and I wish to make special characters like Chinese not encoded, but when I config global setting it doesn't work, I have to apply setting for single JsonSerializer.Serialize method, wondering did I do something wrong?
My dotnet version is 6.0.301 and I'm using VS 2022 17.2.1.
Program.cs

using System.Text.Encodings.Web;
using System.Text.Unicode;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Encoder = JavaScriptEncoder
        .Create(UnicodeRanges.BasicLatin, UnicodeRanges.CjkUnifiedIdeographs);
});
builder.Services.AddServerSideBlazor();
...

Index.razor


@jsonString

@code {
    public string jsonString { get; set; }
    protected override async Task OnInitializedAsync()
    {
        jsonString = JsonSerializer.Serialize(new
            {
                Name = "你的名字"
            });
    }
}

image.png


Original Comments

Feedback Bot on 7/4/2022, 10:35 AM:

(private comment, text removed)


Original Solutions

(no solutions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Author: Migration Bot 🤖The issue was created by a issue mover bot. The author may not be the actual author.DocsThis issue tracks updating documentationarea-ui-renderingIncludes: MVC Views/Pages, Razor Views/Pages

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions