2
 <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
 <meta name="description" content="@("Du lịch chữa bệnh ở Thái Lan là hình thức du lịch đẳng cấp kết hợp du lịch, spa và chữa bệnh")" />

why would it put out wrong unicode string

 <meta name="description" content="Du lịch chữa bệnh ở Th&#225;i Lan l&#224; h&#236;nh thức du lịch đẳng cấp kết hợp du lịch, spa v&#224; chữa bệnh" />

I've tried new HtmlString("Du lịch...") or Html.Raw("Du...") and no luck so far

What's wrong with that ? please give an advice. I'm using asp.net mvc 5.0

Without @, it works fine, just as expected !

Other thread has the same result but no answer, Disable encoding of unicode characters in ASP.NET-MVC3

4
  • try <%= HttpUtility.HtmlEncode("your text") %> Commented Jan 30, 2014 at 10:20
  • same, nothing change. Commented Jan 30, 2014 at 10:24
  • Replace '@' with '@@'.. and see if problem solves. Commented Jan 30, 2014 at 10:30
  • content="@@("Du lịch chữa bệnh ở Thái Lan là hình thức du lịch đẳng cấp kết hợp du lịch, spa và chữa bệnh")" /> => "@" is not valid at the start of a code block. Only identifiers, keywords, comments, "(" and "{" are valid. Commented Jan 30, 2014 at 10:32

4 Answers 4

1

You can configure default encoding behaviour in your ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<WebEncoderOptions>(options => 
            {
                options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
            });
}

This will render non-encoded unicode characters on the html page. Source https://github.com/aspnet/HttpAbstractions/issues/315

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

Comments

0

This is because of html encoding razor by default do html encoding. Try this

content="@Html.Raw("Du lịch chữa bệnh ở Thái Lan là hình thức du lịch đẳng cấp kết hợp du lịch, spa và chữa bệnh")"

@Html.Raw(""); render the input string as raw html

1 Comment

What if the string contains some character such as <?
0

The string is not wrong. &#225; will be read by any web browser as the character á. In terms of the information set a browser will parse into the DOM, the two lines:

<meta name="description" content="Du lịch chữa bệnh ở Th&#225;i Lan l&#224; h&#236;nh thức du lịch đẳng cấp kết hợp du lịch, spa v&#224; chữa bệnh" />
<meta name="description" content="Du lịch chữa bệnh ở Thái Lan là hình thức du lịch đẳng cấp kết hợp du lịch, spa và chữa bệnh" />

are absolutely identical.

It's a bit weird that ASP.NET's default HTML encoder chooses to write &#...; character references for characters in the range U+0080 to U+00FF but not other Unicode characters, but in general an HTML encoder can choose to encode any character it fancies and the output will still be correct.

nemesv's answer in the question you linked shows how you can change this behaviour by overriding encoderType, but there is almost never any reason to care. Any other tool that consumes your output and does not treat &#225; and á in an attribute value as the same character, is broken.

Comments

0

Try it in webconfig(asp.net mvc5) <globalization fileEncoding="utf-8" /> </system.web>

1 Comment

Further explanation and any backup links regarding your answer will be a great help for future researchers that may stumble upon the same problem. You might want to explain how your answer helps in the problem. Check How do I write a good answer?.

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.