locked
encoding issues RRS feed

  • Question

  • User559506553 posted

    Hi All,

    We had classis ASP application (which caters services to global users) and we set encoding to Windows-1252, and its been there for long time. And few years back we migrated to ASP.Net but left the encoding to windows-1252 ( to support existing data)

    Recent days we have encountered problems with Japaneese and Chinese users, so decided to change encoding to UTF-8,

    So what is best approach to minimize the issues for existing data ( which was stored in windows-1252 ).

    Thnanks

    Mogili.

    Friday, September 16, 2011 10:31 AM

Answers

  • User1622957740 posted

    Not sure, what you mean by best approach. If you switch your data to UTF-8, clients that receive the data and expect Windows-1252 will break unless they are automatically sniffing the encoding properly.

    Changing the encoding is easy enough to do in web.config with:

    <system.web>
       <globalization  responseEncoding="utf-8"/>
    </system.web>

    One thing you can do is allow for a flag (querystring flag or request header) on the request to specify which encoding should be returned. Then in your actual code that generates output you can set Response.Encoding as needed in code.

                var q = Request.QueryString["enc"];
    
                if (!string.IsNullOrEmpty(q) && q == "utf8")
                {
                    Response.ContentType = "text/html;charset=Utf-8";
                    Response.ContentEncoding = Encoding.UTF8;
                }
                else
                {
                    Response.ContentType = "text/html;charset=Windows-1252";
                    Response.ContentEncoding = Encoding.GetEncoding(1252);
                }

    This way you can serve responses both ways overriding whatever global settings are made to ASP.NET.

    +++ Rick ---

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 20, 2011 7:54 PM

All replies

  • User1622957740 posted

    Not sure, what you mean by best approach. If you switch your data to UTF-8, clients that receive the data and expect Windows-1252 will break unless they are automatically sniffing the encoding properly.

    Changing the encoding is easy enough to do in web.config with:

    <system.web>
       <globalization  responseEncoding="utf-8"/>
    </system.web>

    One thing you can do is allow for a flag (querystring flag or request header) on the request to specify which encoding should be returned. Then in your actual code that generates output you can set Response.Encoding as needed in code.

                var q = Request.QueryString["enc"];
    
                if (!string.IsNullOrEmpty(q) && q == "utf8")
                {
                    Response.ContentType = "text/html;charset=Utf-8";
                    Response.ContentEncoding = Encoding.UTF8;
                }
                else
                {
                    Response.ContentType = "text/html;charset=Windows-1252";
                    Response.ContentEncoding = Encoding.GetEncoding(1252);
                }

    This way you can serve responses both ways overriding whatever global settings are made to ASP.NET.

    +++ Rick ---

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 20, 2011 7:54 PM