Non-UTF-8 text encoding in parameters in ASP.NET MVC

Background

I have a web application that uses ISO-8859-1 . When I pass parameters using Html.ActionLink() , the value is decoded before 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 get in my controller, UTF-8 , not ISO-8859-1 :

TestController :

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

Question

Why is the string not decoded before Cosméticos ?

+4
source share
4 answers

I found this problem and workaround: the value I get 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 "É".

To work around this problem, copy the string to byte [], and then use System.Text.Encoding.Convert ().

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

0
source

Are your aspx files saved physically in iso-8859-1?

"File / Save Xyz Like" And click to the right of the save button to have more encoding options to save the file to ..

A guess

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

A few things you might want to consider.

Firstly, if you have not read this yet, I highly recommend reading Joel Spolsky's article Absolute Minimum. Every software developer should absolutely have a positive knowledge of Unicode and Character Sets (No Excuses!) ') He creates the basis for learning character encoding and programming.

Secondly, looking at the documents on the globalization element in the web.config file , there seem to be ways (by chance?) To override the specified encoding scheme. From the docs:

requestEncoding

Defines the intended encoding of each incoming request, including published data and the query string. If the request comes with a request header containing the Accept-Charset attribute, it overrides requestEncoding in the configuration. The default encoding is UTF-8, specified in the <globalization> tag included in the Machine.config file created during the installation of the .NET Framework. If the request encoding is not specified in the Machine.config or Web.config file, the encoding uses the locale of the regional settings of the computer by default. In single-server applications, requestEncoding and responseEncoding must be the same. For a less common case (multi-server applications, where the server encodings are different by default), you can vary the encoding of the request and response using the local Web.config files.

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

0
source

All Articles