Razor - @ Html.Raw () still encodes in meta tag attributes

When I use @ Html.Raw (mystring) normal, it correctly displays an example:

@{ ViewBag.Title = "My Site&reg;"; } <title>@Html.Raw(ViewBag.Title)</title> 

It will display <title>My Site&reg;</title> correctly, but when I use it in the attribute:

 <meta name="description" content="@Html.Raw(ViewBag.Title)" /> 

It displays <meta name="description" content="My Site&amp;reg;" /> <meta name="description" content="My Site&amp;reg;" /> , which is incorrect, because then it will not display the registered character.

How do you fix this behavior?

+7
source share
3 answers

As patridge pointed out in his answer, you can simply include attribute markup in the .Raw parameter.

In your case, this will lead to something like the following:

 <meta name="description" @Html.Raw("content=\"My Site&amp;reg;\"") /> 
+6
source

You can do this in your _layout.cshtml:

 <title>@{var title = new HtmlString(ViewBag.Title);}@title</title> 

It worked for me.

0
source

I think this is the tip of the solution:

 @{ ViewBag.Title = "My Site&reg;"; } <title>@Html.Raw(ViewBag.Title)</title> <meta name="description" content="@HttpUtility.HtmlDecode(ViewBag.Title)" /> 
0
source

All Articles