2

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

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
ShaneKm
  • 20,823
  • 43
  • 167
  • 296

5 Answers5

2

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.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
2

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • how would i decode it?? function checkAndHandleMessageFromHeader(request) { var msg = request.getResponseHeader('X-Message'); if (msg) { displayMessage(msg, request.getResponseHeader('X-Message-Type')); } } – ShaneKm May 18 '11 at 13:42
  • @Shane I don't know what the function is to URLDecode data in ASP.net (overlooked that tag) but I'm sure it exists in the basic library. – Pekka May 19 '11 at 11:55
1

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Is it not theoretically possible, though? According to [this](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html), `If a character set other than ISO-8859-1 is used, it MUST be encoded in the warn-text using the method described in RFC 2047 [14].` ... although I personally would just URLencode it and be done with it – Pekka May 18 '11 at 12:59
  • @Pekka, I wouldn't experiment with this. I would always encode so that ASCII is used. – Darin Dimitrov May 18 '11 at 13:01
  • Darin, how would i add some content to message-body? – ShaneKm May 18 '11 at 13:13
  • @Shane Km, that's the contents sent by the server. Usually the HTML you see in the browser. – Darin Dimitrov May 18 '11 at 13:14
  • yes, but how would i add it to my response in filterContext.HttpContext? – ShaneKm May 18 '11 at 13:55
  • 1
    @Shane Km, your question is extremely unclear. What are you trying to achieve? What is the purpose of the action filter? Why are you trying to append HTTP headers? – Darin Dimitrov May 18 '11 at 14:06
  • 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? thanks – ShaneKm May 19 '11 at 05:18
1

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.

ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • To decode the message you can also use the standard JavaScript function: `decodeURIComponent(msg)`. No need for a plugin. More about it here: http://stackoverflow.com/a/3803742/114029 – Leniel Maccaferri May 28 '12 at 22:12
0

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

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

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339