简体   繁体   English

.net核心类库中的不同字符编码

[英]Different character encoding in .net core class library

I'm developing an API and are bumping in to a issue with character encoding. 我正在开发一个API,并且遇到了字符编码的问题。 Consider the two following projects in a solution running .NET Core 2.1: 在运行.NET Core 2.1的解决方案中考虑以下两个项目:

  • MyApi.Web MyApi.Web
  • MyApi.Services MyApi.Services

This is the code from my controller in Web 这是我在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: 当我从邮递员那里称这个api时,我得到以下结果:

{
    "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). 它必须与它们有关,在不同的项目(Web项目和类库)中创建字符串。

Okay, so I finally figured this out. 好的,所以我终于搞清楚了。

The issue was with file encodings in Visual Studio. 问题在于Visual Studio中的文件编码。 My first file was saved in Unicode while the second file in the service project was saved as ANSI. 我的第一个文件以Unicode格式保存,而服务项目中的第二个文件保存为ANSI。 It seems like you can't set any options in Visual Studio as to what encoding to always save as. 看起来您无法在Visual Studio中设置任何选项,以便始终保存为哪种编码。 This is instead selected from your Windows language settings. 而是从Windows语言设置中选择此项。

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" 转到Tools -> Options -> Environment -> Documents然后选中“无法保存在代码页中时将文档另存为Unicode”

  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. 在搜索中搜索并替换unicode字符替换字符 并将其换成正确的字符。

Maybe not an optimal approach but it solved the problem! 也许不是最佳方法,但它解决了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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