4

Background

I have a web application that uses ISO-8859-1 encoding. When I pass parameters using Html.ActionLink(), the value is decoded to UTF-8:

Web.config:

<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1"
               fileEncoding="iso-8859-1" />

Index.aspx

This is a <%= Html.ActionLink("test", "Read", new { name="Cosméticos" }) %>

generates the following:

This is a <a href="/Intranet/Read?name=Cosm%C3%A9ticos">test</a>

The problem is the value I receive in my controller is UTF-8, not iso-8859-1:

TestController:

public ActionResult Read(string name) {
  //name is "Cosméticos" here!
}

Question

Why the string is not decoded to Cosméticos?

1
  • Thanks, George - that's much better. Commented Dec 22, 2009 at 13:47

4 Answers 4

1

Does your aspx files are physically saved in iso-8859-1?

"File / Save Xyz As" And click at the right of the save button to have more encoding options to save your file in..

A guess

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, my files are saved in iso-8859-1. But I don't think this is the problem, because "Cosméticos" is coming from my database.
The thing with encodings, it have to strictly be the same across each layer of your app., or be converted from one to another if there's a mismatch between 2 layers. Does your DB is in iso-8859-1? Maybe yes, but maybe values were in UTF-8 before putting them in!? Try to convert your DB values in iso-8859-1 when outputting to you aspx pages to see if it helps.. You have to find the step where the encoding breaks and it could be somewhere else before your MVC action parameters.
Also, I run a french/english site so I have lots of latin characters. I prefer using UTF-8 anyway to cover a broader range of characters.
My db is in iso-8859-1 format. But as you can see in my Index.aspx file, the string is correctly url encoded so it doesn't matter anymore if my file or db is saved in iso-8859-1. It seems that the problem is when the framework decodes it.
1
public static string ActionLinkNoEncode(this HtmlHelper htmlHelper, string linkText, ActionResult action )
{
    var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);   
    var url = Uri.UnescapeDataString(urlHelper.Action(action)).ToLowerInvariant();  
    var linkTagBuilder = new TagBuilder("a");   
    linkTagBuilder.MergeAttribute("href", url);
    linkTagBuilder.InnerHtml = linkText;
    return linkTagBuilder.ToString();
}

Comments

0

I found the problem and the workaround: the value I receive is UTF-8, but if I try to use System.Text.Encoding.UTF8.GetBytes(name) it converts the characters "é" to UTF-8 values instead of "É".

The workaround is to copy the string to a byte[] and then use System.Text.Encoding.Convert().

I don't know if this is the best way, but now everything is working for me.

Comments

0

A few things you might want to consider.

First, if you haven't already read it -- I highly recommend reading Joel Spolsky's article 'The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)' It sets the stage for learning about character encoding and programming.

Second, looking at the docs on the globalization element in the web.config it sounds like there are ways to (accidentally?) override the specified encoding scheme. From the docs:

requestEncoding

Specifies the assumed encoding of each incoming request, including posted data and the query string. If the request comes with a request header containing an Accept-Charset attribute, it overrides the requestEncoding in configuration. The default encoding is UTF-8, specified in the <globalization> tag included in the Machine.config file created when the .NET Framework is installed. If request encoding is not specified in a Machine.config or Web.config file, encoding defaults to the computer's Regional Options locale setting. In single-server applications, requestEncoding and responseEncoding should be the same. For the less common case (multiple-server applications where the default server encodings are different), you can vary the request and response encoding using local Web.config files.

Have you tried using something like Fiddler to see what the Accept-Charset attribute is set to?

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.