question

LingSteven avatar image
0 Votes"
LingSteven asked YiyiYou-MSFT edited

Does razor file support "Shift_JIS" encoding?

Hi,

The visual studio create the .cshtml file with UTF-8 encdoing. I save it ith "shift-jis" encoding by using 'Advanced save options..' in visual studio.

Now, it shows the Japanese words is displayed in garbled code.


             @Html.DisplayName("メニューを生成します。")

I want to know if the razor engine support "shift-jis" encoding.


121951-1.jpg

Thanks

dotnet-aspnet-core-mvc
1.jpg (21.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@YiyiYou-MSFT Could you confirm it?

0 Votes 0 ·
YiyiYou-MSFT avatar image
0 Votes"
YiyiYou-MSFT answered YiyiYou-MSFT edited

Hi,@LingSteven,

why not try to use UTF-8 in Advanced save options ?With UTF-8, the Japanese words can be shown correctly.
Here is my setting:
122889-image.png


result:
122900-image.png





If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best Regards,
YiyiYou


image.png (9.6 KiB)
image.png (2.0 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Because of my project's special requirement. I want to know if it supports shift-jis. If it doesn't support shift-jis, I won't insist on using shift-jis, I will choose to use utf8. Thanks.

0 Votes 0 ·

Hi,@LingSteven,
I have updated my answer,when I use utf-8,it can show the words correctly as the image shown.And you can also refer to what Bruce-SqlWork said ,shift-jis is not supported.

1 Vote 1 ·
cheong00 avatar image
0 Votes"
cheong00 answered AgaveJoe commented

Razor do not limit the code page for any particular one, however you should check whether your master view contains something like the following:

 <meta charset="UTF-8" />

Also, there could be similar HTTP header specifying character set should be UTF-8 that you need to change if you need override.

Note that these two change are site-wide changes. You may break other things if you do that. Always do full check after this kind of change to make sure things working okay.

======

That said, since UTF-8 can represent all characters in Shift-JIS, do you have any particular reason to use it?

Even if you need to support Shift-JIS data from database, you can always Encoding.Convert() your encoding in code before display/save it.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In the '_Layout.cshtml' file, I've changed the charset to 'Shift_JIS' and rewrite the response header's like the following:

this.Response.ContentType = $"text/html;charset=Shift_JIS";

It still shows the Japanese words in garbled code.

0 Votes 0 ·

The Response doens't have ContentEncoding property in asp.net core.

0 Votes 0 ·

Encoding is just a mapping between values and print characters. Your approach changes the encoding of the text file you're saving. Now, anyone that wants to display the view must use the same encoding otherwise the print characters will not display correctly.

As recommended above, you should use a standard encoding like UTF-8 which includes Japanese characters.

It is not clear why you are changing the encoding of a View file. What problem are you trying to solve???


0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered LingSteven commented

Html has limited character set support. UTF-8, UTF-16, Windows-1252 and ISO-8859 only.

You should use UTF-16 see

https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-encoding

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Bruce-SqlWork Did you mean the razor doesn't support 'shift-jis' encoding?

0 Votes 0 ·

@Bruce-SqlWork Could you give me a response?

0 Votes 0 ·