MVC Razor语言 - 未在页面元标记中呈现的特殊字符



我正在尝试在我的MVC 4应用程序中添加页面元标记。这是我的布局标题:

<title>@Html.Raw(Model.CurrentPageMetaTags.Title)</title>
<meta name="description" content="@Html.Raw(Model.CurrentPageMetaTags.Description)" />
<meta name="keywords" content="@Html.Raw(Model.CurrentPageMetaTags.Keywords)" />

从模型填充的元标记为:

Title = "Current page's title";
Description = "Current page's description";
Keywords = "Current page's keywords";

但是当涉及到浏览器时,它显示元标记如下:

<title>Current page's title</title>
<meta name="description" content="Current page&#39;s description" />
<meta name="keywords" content="Current page&#39;s keywords" />

标题没有问题,但特殊字符无法呈现描述和关键字!

将以下行添加到 Web.config 文件内的 httpRuntime 行。

encoderType="System.Web.Security.AntiXss.AntiXssEncoder"

<httpRuntime encoderType="System.Web.Security.AntiXss.AntiXssEncoder" requestValidationMode="2.0" enableVersionHeader="false" targetFramework="4.7.2" maxRequestLength="51200" fcnMode="Single" />

我认为这可能会对您有所帮助:

@Html.Raw(WebUtility.HtmlDecode(Model.CurrentPageMetaTags.Title))

干杯

最新更新