无法从asp.net中的代码后面添加元数据 [英] unable to add meta data from code behind in asp.net

查看:82
本文介绍了无法从asp.net中的代码后面添加元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我无法从后面的代码中在页面中添加元数据关键字,描述等.
即时通讯收到此错误:

Hi ,
im unable to add meta data keywords , descriptions , etc in page from code behind.
im getting this error :

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).




而以下是代码:




while the following is the code :

HtmlMeta tag = new HtmlMeta();
tag.Name = "description";
tag.Content = "My description for this page";
Header.Controls.Add(tag);




我也尝试过使用page.metkeywords,page.header.keyword ..
但是出现相同的错误




i have also tried using page.metkeywords , page.header.keyword..
but getting same error

推荐答案

假设您的页面没有母版页,则可以像这样使用它:

Assuming your page does not have a masterpage, you can just use it like this:

Header.Keywords = "key1, key2";
      Header.Controls.Add(new HtmlMeta(){Name = "description",Content = "my description"});



这实际上将呈现:



and that will actually render:

<head><title>
</title>
<meta name="description" content="my description" /><meta name="keywords" content="key1, key2" />
</head>



您需要带有runat ="server"的标头才能正常工作.如果您有母版页,则需要从那里开始.

但我认为您的错误是其他问题.



you need the header with runat="server" for this to work. if you have a master page you need to do it from there.

but I think your error is about something else.


在引用母版页中的网址时使用了<%#而不是<%=.
used <%# instead of <%= in referencing urls in master pages


这篇关于无法从asp.net中的代码后面添加元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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