简体   繁体   English

使用剃刀在html页面标题中输出meta标签

[英]Output meta tag in html page header using razor

I have a razor helper that is meant to create a meta tag on the fly, however all I am seeing in the source of the page are asci characters.. the page source should read <meta name='robots' content='noindex' /> Much appreciate all replies and suggestions. 我有一个剃刀助手,旨在即时创建一个meta标签,但是我在页面源代码中看到的都是asci字符。.页面源代码应显示为<meta name='robots' content='noindex' />非常感谢所有答复和建议。 Thank you! 谢谢!

Helper - 帮手-

@helper WriteMetaRobots(DynamicNode siteRoot)
{
var robotValue = "noindex";
var robots = !string.IsNullOrEmpty(CurrentModel.GetPropertyValue("metaRobots")) ? "<meta name='robots' content='"+robotValue+"' />" : "";
    @robots
}

Page source - 网页来源-

&lt;meta name=&#39;robots&#39; content=noindex /&gt;

Razor encodes html to prevents xss attacks. Razor对html进行编码以防止xss攻击。 So you'll have to explicity tell razor not to encode the html tags. 因此,您必须明确告诉razor不要对html标签进行编码。 Instead of @robots you'll want to use @Html.Raw(robots) here 您将要在此处使用@Html.Raw(robots)而不是@robots

More information about xss prevention can be found here 可以在这里找到有关xss预防的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM