ISO-8859-1 to UTF8 in ASP.NET 2

Posted by Gordon Carpenter-Thompson on Stack Overflow See other posts from Stack Overflow or by Gordon Carpenter-Thompson
Published on 2009-06-18T11:19:13Z Indexed on 2010/03/23 17:43 UTC
Read the original article Hit count: 538

Filed under:
|
|
|

We've got a page which posts data to our ASP.NET app in ISO-8859-1

<head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <title>`Sample Search Invoker`</title>
</head>
<body>

<form name="advancedform" method="post" action="SearchResults.aspx">
    <input class="field" name="SearchTextBox" type="text" />
    <input class="button" name="search" type="submit" value="Search &gt;" />
</form>

and in the code behind (SearchResults.aspx.cs)

System.Collections.Specialized.NameValueCollection postedValues = Request.Form;
String nextKey;
for (int i = 0; i < postedValues.AllKeys.Length; i++)
{
    nextKey = postedValues.AllKeys[i];

    if (nextKey.Substring(0, 2) != "__")
    {
        // Get basic search text
        if (nextKey.EndsWith(XAEConstants.CONTROL_SearchTextBox))
        {
            // Get search text value
            String sSentSearchText = postedValues[i];

            System.Text.Encoding iso88591 = System.Text.Encoding.GetEncoding("iso-8859-1");
            System.Text.Encoding utf8 = System.Text.Encoding.UTF8;

            byte[] abInput = iso88591.GetBytes(sSentSearchText);

            sSentSearchText = utf8.GetString(System.Text.Encoding.Convert(iso88591, utf8, abInput));

            this.SearchText = sSentSearchText.Replace('<', ' ').Replace('>',' ');
            this.PreviousSearchText.Value = this.SearchText;
        }
    }
}

When we pass through Merkblätter it gets pulled out of postedValues[i] as Merkbl?tter The raw string string is Merkbl%ufffdtter

Any ideas?

© Stack Overflow or respective owner

Related posts about encoding

Related posts about ASP.NET