2

I have an issue with encoding between JavaScript and C#. I'm creating an url in JavaScript like this:

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

Internally this URL is rewritten to the following:

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

That is the encoded url and that is correct.

Now I want to get the parameter out of the url with C#'s Request like the following:

Request["ProjectName"]

But ProjectName returns me:

Schockville-Attert - Rue des Blés

When I look into my Request property I see that my %C3%A9 has become %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.

I already tried: encodeURIComponent, trouble with ü,ç,İ,ı,ğ,ö

I triend unescaping my string but this won't work either. Tried to encode my string to UTF-8 and didn't work.

Can somebody help me?

3
  • You do want to be using encodeURIComponent when setting the href rather than relying on it happening "automatically", e.g., window.location.href = 'url/home/projects/ProjectName/' + encodeURIComponent('Schockville-Attert - Rue des Blés'); But I suspect that's only part of the problem. Commented May 31, 2013 at 8:34
  • I've already tried that but that gives me the encoded URI like I showed above and still the same problem on decoding the URI in C# Commented May 31, 2013 at 8:59
  • @ Jurgen: As I said, I don't think it's the solution, it's just part of it. I wouldn't rely on that encoding being correctly done automatically. But the underlying problem remains. Commented May 31, 2013 at 9:25

1 Answer 1

0

The string is coming in as Encoding.Default, try the code below:

byte[] utf8Bytes = System.Text.Encoding.Default.GetBytes(myString);
myString = System.Text.Encoding.UTF8.GetString(utf8Bytes);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.