在C#中添加Meta标签编程方式 [英] Adding Meta Tag Programatically in c#

查看:335
本文介绍了在C#中添加Meta标签编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式添加一个<&荟萃GT; 。它工作正常时,有在 RUNAT = 元素服务器。 ASPX

I'm trying to programmatically add a <meta>. It is working fine when there is Head element with runat = "server" in the .aspx page .

在code后面的是:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Header.Controls.Add(meta);

但我在其中包含code块如&LT head标签的一些脚本;%...%GT; 。所以我不能保持 =服务器值。

问题是我必须以编程方式添加meta标签,因为它依赖于从数据库中值。

The problem is I have to add the meta tag programmatically because it depends on a value from the database.

有什么办法来解决这个问题,使我的头元素中的脚本照常工作,我可以以编程方式添加meta标签?

Is there any way to solve this issue so that my script inside the head element works as usual and I can add meta tag programmatically?

推荐答案

好吧,我测试的<一个href=\"http://stackoverflow.com/questions/1432431/adding-meta-tag-programatically-in-c/1432450#1432450\">answer通过 veggerby ,它完美的作品:

OK, I tested the answer by veggerby, and it works perfectly:

&LT;报头GT; 部分:

<asp:PlaceHolder id="MetaPlaceHolder" runat="server" />

请注意,Visual Studio中可能会显示在占位符标记一个警告,因为它不会被识别为头内一个已知的元素,但是你可以忽略这一点。它的工作原理。

Note that Visual Studio might show a warning on the PlaceHolder tag because it is not recognised as a known element inside the header, but you can ignore this. It works.

在C#code:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
MetaPlaceHolder.Controls.Add(meta);

或者(因为你使用已经有code块&LT;%%&GT; 在头一节),你可以直接标记元从其获取唯一的价值从服务器端:

Alternatively (since you already have code blocks using <% %> in your header section) you can tag the meta directly and retreive only the value from server side:

<meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />

这篇关于在C#中添加Meta标签编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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