Reputation: 6787
The source code:
@{ ViewBag.Title = "سلام علیک"; }
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>@ViewBag.Title</title>
</head>
<body>
<div class="container" dir="rtl">
@RenderBody()
</div>
</body>
</html>
It's well rendered in browser but I want the same text in html source (for some search engine optimizer software)
And the output:
<!DOCTYPE html>
<html>
<head>
<title>سلام علیک</title>
</head>
<body>
...
</body>
</html>
Upvotes: 10
Views: 4058
Reputation: 159
Even in Blazor Web assembly, I solved this problem by copying the below line inside program.cs file:
builder.Services.AddSingleton<HtmlEncoder>(HtmlEncoder.Create(allowedRanges: new[ ] { UnicodeRanges.BasicLatin, UnicodeRanges.Arabic }));
If you are working with server projects or previous version's of core that has startup.cs you can write like this:
services.AddSingleton<HtmlEncoder>( HtmlEncoder.Create( allowedRanges: new[ ] { UnicodeRanges.BasicLatin, UnicodeRanges.Arabic } ) );
Upvotes: 0
Reputation: 56520
Because, by default, the HTML encoding engine will only safelist the basic latin alphabet (because browsers have bugs. So we're trying to protect against unknown problems). The &XXX values you see still render as correctly as you can see in your screen shots, so there's no real harm, aside from the increased page size.
If the increased page size bothers you then you can customise the encoder to safe list your own character pages (not language, Unicode doesn't think in terms on language)
To widen the characters treated as safe by the encoder you would insert the following line into the ConfigureServices() method in startup.cs;
services.AddSingleton<HtmlEncoder>(
HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin,
UnicodeRanges.Arabic }));
Arabic has quite a few blocks in Unicode, so you may need to add more blocks to get the full range you need.
Upvotes: 32
Reputation: 4456
You have to set the character encoding for the response to be UTF-8 in order to be able to output the non-Unicode characters like Arabic
<configuration>
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
</system.web>
</configuration>
Upvotes: -2
Reputation: 6203
For non ACII
chars, I recommend to use UTF-8
as the charset. You can add this line into your HTML file (shared layout). in the <head>
tag.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
And set dir="rtl" and lang="ar", use like :
<p dir="rtl" lang="ar" ">سلام علیک</p>
Also You can use ViewData["Title"]
instead ViewBag.Title
it should give same result.
Character encodings in HTML-wiki
Upvotes: -1