4

I have a problem when using special characters in meta and alt text in Umbraco.

I am testing with Meta description and Alt text like this: Test test æøå

And it generates this output:

<meta name="description" content="Test test &#230;&#248;&#229;">

<img src="#" alt="Test test &#230;&#248;&#229;" />

If I insert the same text in title tags and normal content, then the output is just perfect.

The code that is generating the meta and title tags looks like this:

<!DOCTYPE html>
<html lang="da">
<head>
  <meta charset="utf-8">
  <meta name="description" content="@Umbraco.Field("pageDescription")">
  <title>@Umbraco.Field("pageTitle")</title>
</head>

The files are saved as utf-8 using Notepad++.

If I insert æøå directly in the HTML, then it shows æøå without any problems.

I have also tried this:

<p>@Umbraco.Field("pageDescription")</p>

And then it shows "æøå" correctly.

Does anybody know what I am doing wrong?

Thanks in advance!

// René

1 Answers1

4

Looks like this is a "feature" of Razor that it will always HTML encode attributes. See: Razor - @Html.Raw() still encoding &amp; in meta tag attributes

So to work around this, you can do the following:

<meta name="description" @Html.Raw("content=\""+ @Umbraco.Field("pageDescription") + "\"") />
<title>@{ var title = new HtmlString(Umbraco.Field("pageTitle").ToString());}@title</title>

It's not pretty, but it works.

Community
  • 1
  • 1
sebastiaan
  • 5,870
  • 5
  • 38
  • 68
  • Hi Sebastiaan. Your solution outputs this with a double quote before content. If I remove it then it does not work. Any suggestions? Maybe this problem is not an issue, see this link: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/63230-Special-characters-in-meta-and-alt-text Do you agree? – VanDerPrygel Mar 18 '15 at 13:52
  • @VanDerPrygel Ah I see, I've now updated it with an even more ugly solution that actually works. I guess it's correct that it doesn't actually matter at all, but now you know how to do it anyway. – sebastiaan Mar 18 '15 at 17:31
  • Can't you just use @Html.Raw(Model.Content.GetPropertyValue("pageDescription")) instead of this ugly @Umbraco.Field concat's ? – dampee Mar 18 '15 at 18:07
  • Nope, see http://stackoverflow.com/questions/14901550/razor-html-raw-still-encoding-amp-in-meta-tag-attributes – sebastiaan Mar 18 '15 at 20:12
  • Of course instead of `Umbraco.Field` you can use `Model.Content.GetPropertyValue` but that's beside the point. – sebastiaan Mar 18 '15 at 20:13