wmasm
wmasm

Reputation: 212

Encoding non UTF-8 text in Parameters in ASP.NET MVC

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?

Upvotes: 4

Views: 6083

Answers (4)

Dan Esparza
Dan Esparza

Reputation: 28367

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?

Upvotes: 0

Wichai Damalee
Wichai Damalee

Reputation: 11

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();
}

Upvotes: 1

wmasm
wmasm

Reputation: 212

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.

Upvotes: 0

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

Upvotes: 1

Related Questions