简体   繁体   中英

Different character encoding in .net core class library

I'm developing an API and are bumping in to a issue with character encoding. Consider the two following projects in a solution running .NET Core 2.1:

  • MyApi.Web
  • MyApi.Services

This is the code from my controller in Web

[HttpGet("text")]
public async Task<JsonResult> GetText()
{
    string culture = CultureInfo.CurrentCulture.DisplayName;
    string encoding = Encoding.Default.ToString();

    string controllerText = culture + " " + encoding + " åäö";
    string serviceText = await _slotService.GetText();

    return _responseBuilder.Success("ControllerText: " + 
    controllerText + " ServiceText: " + serviceText);
}

This is the code from my service:

    public async Task<string> GetText()
    {
        string culture = CultureInfo.CurrentCulture.DisplayName;
        string encoding = Encoding.Default.ToString();
        return culture + " " + encoding + " åäö";
    }

When I call this api from postman I get the following result:

{
    "message": "ControllerText: sv (SE) 
    System.Text.UTF8Encoding+UTF8EncodingSealed åäö ServiceText: sv 
    (SE) System.Text.UTF8Encoding+UTF8EncodingSealed 
    \ufffd\ufffd\ufffd"
}

Why would the controller and service give different results for the "åäö" characters? Even though they seem to have the same culture and encoding. It must have something to do with them creating the strings in different projects (web project, and class library).

Okay, so I finally figured this out.

The issue was with file encodings in Visual Studio. My first file was saved in Unicode while the second file in the service project was saved as ANSI. It seems like you can't set any options in Visual Studio as to what encoding to always save as. This is instead selected from your Windows language settings.

I fixed this with the following steps:

  1. Go to Tools -> Options -> Environment -> Documents and check "Save document as Unicode when cannot be saved in codepage"

  2. Run the following command in your project folder:

     Get-ChildItem *.cs -Recurse | ForEach- $content = $_ | Get-Content Set-Content -PassThru $_.Fullname $content -Encoding UTF8 -Force} 
  3. Do a search and replace in your soulution for the unicode character replacement character and swap it for the correct one.

Maybe not an optimal approach but it solved the problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM