ShaneKm
ShaneKm

Reputation: 21328

HttpHeader UTF encoding

I need to store messages in different languages in Http Header:

Response Headers

Cache-Control   private
Content-Type    text/html; charset=utf-8
Server  Microsoft-IIS/7.5
X-AspNetMvc-Version 3.0
X-Message-Type  Success
X-Message   <p>Token wysÅany</p>
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET
Date    Wed, 18 May 2011 12:49:26 GMT
Content-Length  2

But, as you can see X-Message looses it's formatting. it should be "Token wysłany". Help. thanks

EDIT: this is what i have:

        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            var viewData = filterContext.Controller.ViewData;
            var response = filterContext.HttpContext.Response;

            foreach (var messageType in Enum.GetNames(typeof(MessageType)))
            {
                var message = viewData.ContainsKey(messageType)
                                ? (ErrorMessageExtensions.ErrorMessage)viewData[messageType]
                                : null;
                if (message != null) // We store only one message in the http header. First message that comes wins.
                {
                    response.AddHeader("X-Message-Type", messageType);
                    response.AddHeader("X-Message", message.RenderAjax());
                    return;
                }
            }
        }

i'm trying to integrate messaging into my mvc app (like so): http://blogs.taiga.nl/martijn/2011/05/03/keep-your-users-informed-with-asp-net-mvc/ the only problem is that it needs to support multilanguages. What are some other options (or fixes for this solutions that would support other language characters)? thanks

Upvotes: 2

Views: 2198

Answers (5)

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

I was having the same problem with this very messaging project by Martijn Boland. In my case I needed accented characters that we use in the Brazilian Portuguese language as: é á í ó ã õ, etc...

I did this to solve the problem:

response.AddHeader("X-Message", HttpUtility.HtmlEncode(message.ToString()));

and then in the view page (.cshtml) where you show the message:

function displayMessage(message, messageType)
{
    $("#messagewrapper").html('<div class="messagebox ' + messageType.toLowerCase() + '"></div>');

    $("#messagewrapper .messagebox").html(message);

    displayMessages();
}

See that I changed from

$("#messagewrapper .messagebox").text(message);

to

$("#messagewrapper .messagebox").html(message);

That's because now we're getting entity numbers ( HTML markup ) instead of plain text.

Doing so you won't need that additional decode-jquery-plugin you mention in your answer.

Upvotes: 2

ShaneKm
ShaneKm

Reputation: 21328

Ok, i figured it out. In order to use other language characters I do the following:

response.AddHeader("X-Message", HttpUtility.UrlEncode(message.RenderAjax(), Encoding.UTF8));

Then, i use this plugin: http://urldecoderonline.com/javascript-url-decode-jquery-plugin.htm to urldecode the string. This generates correct output :).

thanks everyone for help.

Upvotes: 1

Pekka
Pekka

Reputation: 449495

As has been pointed out, headers should contain only US-ASCII or ISO-8859-1 characters.

Depending on who is going to read the header, consider urlencode() ing the message.

That will make sure you have only ASCII characters in the header. As long as you're in UTF-8 all the way, it will work fine.

Of course, you need to do a urldecode() on it so it becomes readable again.

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337580

HttpHeaders can only contain ISO-8859-1 characters (a full list available here).

I don't believe there are any work arounds for this.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

The HTTP headers use the US-ASCII encoding, so you should avoid sending characters in the headers which are outside of this encoding. The message body can of course use any encoding.

Upvotes: 1

Related Questions