简体   繁体   English

JavaScript转换为C#UTF-8编码问题

[英]JavaScript to C# UTF-8 encoding issue

I have an issue with encoding between JavaScript and C#. 我在JavaScript和C#之间编码存在问题。 I'm creating an url in JavaScript like this: 我在JavaScript中创建如下网址:

window.location.href = 'url/home/projects/ProjectName/Schockville-Attert - Rue des Blés';

Internally this URL is rewritten to the following: 在内部将此URL重写为以下内容:

'url/home/projects/ProjectNameSchockville-Attert%20-%20Rue%20des%20Bl%C3%A9s'

That is the encoded url and that is correct. 那是编码的URL,并且是正确的。

Now I want to get the parameter out of the url with C#'s Request like the following: 现在,我想使用C#的请求从url中获取参数,如下所示:

Request["ProjectName"]

But ProjectName returns me: 但是ProjectName返回了我:

Schockville-Attert - Rue des Blés

When I look into my Request property I see that my %C3%A9 has become %u00c3 %u00a9. 当我查看我的Request属性时,我发现我的%C3%A9已变成%u00c3%u00a9。 I search the internet and found out that it is a encoding issue. 我搜索了互联网,发现这是一个编码问题。 This is latin format and it has to be UTF-8 format. 这是拉丁格式,必须为UTF-8格式。

I already tried: encodeURIComponent, trouble with ü,ç,İ,ı,ğ,ö 我已经尝试过: encodeURIComponent,遇到ü,ç,İ,ı,ğ,ö问题

I triend unescaping my string but this won't work either. 我尝试将字符串转义,但是这也不起作用。 Tried to encode my string to UTF-8 and didn't work. 试图将我的字符串编码为UTF-8,但没有用。

Can somebody help me? 有人可以帮我吗?

The string is coming in as Encoding.Default , try the code below: 字符串以Encoding.Default的形式传入 ,请尝试以下代码:

byte[] utf8Bytes = System.Text.Encoding.Default.GetBytes(myString);
myString = System.Text.Encoding.UTF8.GetString(utf8Bytes);

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

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