如何设置在asp.net中动态的meta标签? [英] How to set dynamic meta tags in asp.net?

查看:145
本文介绍了如何设置在asp.net中动态的meta标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给Facebook标签添加到页面。他们必须为不同的用户不同,所以我尽量dynamicaly添加。我已经code是这样的:

I'm trying to add facebook tags to a page. They have to be different for different users, so I try to add them dynamicaly. I have code like this:

<meta name="og:title" content="<%= GetSocialTitle() %>"/>

但是在源页它不揭示,看起来像:

But in source page it doesn't uncovers and looks like:

<meta name="og:title" content="&lt;%= GetSocialTitle() %>"/>

而不是

<meta name="og:title" content="some title">

我试过设置在元code后面

I've tried to set meta in code behind

HtmlMeta tag = new HtmlMeta();
tag.Name = "og:title";
tag.Content = GetSocialTitle();
Page.Header.Controls.Add(tag);

但它抛出异常Controls集合不能修改,因为该控件包含code块(即&LT;%...%>)。这真的包含了,我不能代替他们。那么,如何才能解决这个问题?

But it throws exception "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)". It really contains and I can't replace them. So how can I fix this?

推荐答案

如果你想使用落后删除OG code:标题meta标签,并把=服务器的头标记

If you want to use code behind remove that og:title meta tag and put runat="server" in head tag.

在标记:

<head runat="server">
  ...
</head>

和背后code:

HtmlMeta tag = new HtmlMeta();
tag.Name = "og:title";
tag.Content = GetSocialTitle();
Page.Header.Controls.Add(tag);

如果你不想使用code后面,去除​​头标记RUNAT服务器:

If you don't want to use code behind, remove runat server from head tag :

<head>
  <meta name="og:title" content="<%= GetSocialTitle() %>"/>
  ...
</head>

这篇关于如何设置在asp.net中动态的meta标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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