ASP.NET MVC Resource File String ASP.NET is in a strange encoding format in an HTML source

Let's look at an example: there is a German word: Fußgängerübergänge

What I have:

  • web.config file:

     ... <system.web> <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="auto" uiCulture="auto" /> ... 
  • resource file containing this word:

     <?xml version="1.0" encoding="utf-8"?> <root> ... <data name="TestWord" xml:space="preserve"> <value>Fußgängerübergänge</value> </data> ... </root> 
  • The html page hard-coded the same word and with the help of this resource refers to this word, and also extracts this word from the database:

     ... <head> <meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ... </head> <body> <div>Fußgängerübergänge</div> <div>@Model.SameWordFromDbTable.TestWord</div> <div>@Resources.MyResource.TestWord</div> <div>@MvcHtmlString.Create(Resources.MyResource.TestWord)</div> </body> ... 
  • When I check them in the source code of a web page, they are displayed in two different ways:

     ... <head> <meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ... </head> <body> <div>Fußgängerübergänge</div> <div>Fußgängerübergänge</div> <div>Fu&#223;g&#228;nger&#252;berg&#228;nge</div> <div>Fu&#223;g&#228;nger&#252;berg&#228;nge</div> </body> ... 

Question : what I did wrong, how can I fix this "coding" problem? What if you want the last 2 words to appear in the source code in the same way as the first two?

+5
source share
2 answers

If you want your text not to be encoded in HTML format, you can simply use Html.Raw:

 @Html.Raw(Resources.MyResource.TestWord) 

Is encoding really a problem?

+7
source

This solution works for me:

 alert('@Html.Raw(Resource.ResourceManager.GetString("Contractcheckalert"))'); 

before

Before fix

after

After the fix

0
source

Source: https://habr.com/ru/post/1205496/


All Articles