Share via


UTF-8 characters not displaying if assigned to variables

Question

Thursday, May 22, 2014 9:33 PM

I noticed UTF8 characters (©, ñ, etc) are not displayed by Razor when they're assigned to variables.  They're displaying fine if rendered directly by the page/template. 

Here's my Layout page. 

<!DOCTYPE html>
<HTML lang="en">
<HEAD>
  <TITLE>@Page.Title</TITLE>
    @RenderPage("~/partials/_metaTags.cshtml")
    Copyright © 2014 < this displays properly
    @RenderPage("~/partials/_win8Support.cshtml")
    @RenderPage("~/partials/_iOSSupport.cshtml")
    @RenderPage("~/partials/_cssStylesheets.cshtml")
    @RenderPage("~/partials/_jQuerySupport.cshtml")
    @RenderPage("~/partials/_openGraphFacebook.cshtml")
</HEAD>

<BODY>
  @RenderPage("~/partials/_header.cshtml")
  @RenderPage("~/partials/_headerMenu.cshtml")
  @RenderPage("~/partials/_newsAlert.cshtml")

  <div id="mainbackground">
    <div id="maincontent" class="container">
      @RenderBody()
    </div>
  </div>   

  @RenderPage("~/partials/_footer.cshtml")
</BODY>

@RenderSection("customscript",false)
@RenderPage("~/partials/_adobeTypeKit.cshtml")
@RenderPage("~/partials/_fitVids.cshtml")
@RenderPage("~/partials/_bootstrapJQuerySupport.cshtml")
@RenderPage("~/partials/_refTagger.cshtml")
@RenderPage("~/partials/_googleAnalytics.cshtml")
</HTML>

and here's my _metaTags.cshtml page

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="copyright"   content="@App.Copyright" />  < UTF8 characters doesn't display properly here
    <meta name="description" content="@Page.description">
    <meta name="keywords"    content="@Page.keywords">
    <meta name="author"      content="@Page.author">

I'm using @App.Copyright and assigning it to the meta. 

In my _appStart.cshtml, I have them defined as

@{
// all Application level startup code and initialization goes here...
    App.SiteName      = "Company Name";
    App.URL           = "http://www.mydomain.com";
    App.Copyright     = "Copyright © " + DateTime.Now.Year + " Company name."; < my UTF8 character is here
    App.Description   = "blah blah blah description goes here";
}

What I get is <meta name="copyright" content="Copyright © 2014 ..... 

Is there a special function I need to use here? 

Is this a bug? Thanks. 

All replies (4)

Friday, May 23, 2014 6:05 AM ✅Answered

If you haven't already, try

@Html.Raw(HttpUtility.HtmlDecode(App.Copyright))

Or

@Html.Raw(App.Copyright)

Friday, May 23, 2014 12:05 AM

&#169 is the HTML encoded version of ©

In your _metaTags.cshtml page try

@HttpUtility.HtmlDecode(App.Copyright)

You should probably do that for your description as well if you are expecting any characters there that could get encoded


Friday, May 23, 2014 1:17 AM

Yea, I know. 

Unfortunately, HtmlDecode didn't work. No change. 


Friday, May 23, 2014 9:02 PM

@Html.Raw(App.Copyright)

Works! Thanks!