Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I use @Html.Raw(mystring) normal it renders properly example:

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

Will properly render <title>My Site&reg;</title> but when I use it in an attribute:

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

It renders <meta name="description" content="My Site&amp;reg;" /> which is not correct because then it will not render the registered mark.

How do you correct this behavior?

like image 949
ddilsaver Avatar asked Feb 15 '13 19:02

ddilsaver


People also ask

What does HTML raw () do?

Raw(Object)Returns markup that is not HTML encoded.

Can we use HTML Raw?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. In this article I will explain with an example, how to use Html.

What is @model in razor?

With the Mvc templating and IPublishedContent @Model can be the 'model' part from Mvc but you can also hydrate your own strongly typed models with data from the same objects. In fact, I have recently been contributing to a Mvc hybrid framework where you can combine the two approaches.

What can the @page directive do in a razor page?

@page makes the file into an MVC action, which means that it handles requests directly, without going through a controller. @page must be the first Razor directive on a page. @page affects the behavior of other Razor constructs. Razor Pages file names have a .


1 Answers

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

In your case that would result in something similar to the following:

<meta name="description" @Html.Raw("content=\"My Site&amp;reg;\"") />
like image 120
Justin Helgerson Avatar answered Oct 06 '22 00:10

Justin Helgerson