0

When I use the <%=...%> syntax and pass in a string with non-ascii characters, the string is decoded and outputed as ASCII. For example: <%="é"%> outputs é.

How can I make the views output the encoded characters instead?

1 Answer 1

1

Why do you want to HTML encode? If your page is utf-8 there should be no problem. But if you want to HTML encode you could use <%: in ASP.NET 4.0:

<%: "é" %>

and:

<%= HttpUtility.HtmlEncode("é") %>

in older versions.

Personally I would recommend you to set the requestEncoding and responseEncoding to utf-8 in the <globalization> element in your web.config and put a <meta charset="utf-8" /> (HTML5) tag in the head section of your master page and you should be fine.

Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps I should've mentioned that the template is outputting the contents of a Javascript string. The actual output that I'm getting is &#233. If I use HtmlUtility.HtmlEncode I now get &amp;#233 instead. Since this is in a quoted string, the encoding of the page doesn't seem to mather.
@Tristan St-Cyr, if you intend to pass any string or object to javascript I would recommend you using JSON, like this: var jsVar = <%= new JavaScriptSerializer().Serialize(SomeObject) %>;. This will ensure safe encoding of everything and you could also pass entire object graphs to javascript.
Still not working. <%= new JavaScriptSerializer().Serialize("é") for example outputs: &quot;&#233;&quot;
@Tristan St-Cyr, hmm, weird. It seems that you are HTML encoding the output. Normally it should output var jsVar = "é";. You should not be html encoding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.