剃须刀 - @ Html.Raw()仍编码和放大器;放大器;在meta标签属性 [英] Razor - @Html.Raw() still encoding & in meta tag attributes

查看:129
本文介绍了剃须刀 - @ Html.Raw()仍编码和放大器;放大器;在meta标签属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用@ Html.Raw(MyString的)正常的它呈现正确例如:

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

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

正确呈现&LT;标题&gt;我的网站和放大器;章;&LT; /标题&GT; 但是当我使用它的属性:

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

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

这使得&LT; META NAME =说明内容=我的网站和放大器;放大器;章; /&GT; 因为那不会使注册商标这是不正确的。

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?

推荐答案

作为<一个href=\"http://stackoverflow.com/questions/12321616/why-is-mvc-4-razor-escaping-ampersand-when-using-html-raw-in-a-title-attribute\">patridge指出出他的答案你可以只包括在参数属性标记来 .RAW

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;\"") />

这篇关于剃须刀 - @ Html.Raw()仍编码和放大器;放大器;在meta标签属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆