Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ASP.NET MVC resource file's string is in a strange encoding format in HTML source

Let's see 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"
        />
    ...
    
  • a 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>
    
  • an html page hardcoded the same word and using this resource to reference this word and also retrieving this word from DB:

    ...
    <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 the webpage they appear 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 did I do wrong, how can I fix this "encoding" issue? What should I do if a want the last 2 words appear in the source code as same as the first two?

like image 557
Bundy Avatar asked Oct 25 '14 13:10

Bundy


1 Answers

If you want to prevent your text from being HTML encoded, you can simply use Html.Raw:

@Html.Raw(Resources.MyResource.TestWord)

Is the encoding actually a problem, though?

like image 126
Alan Avatar answered Sep 18 '22 02:09

Alan