ASP.net render meta tag validly

自作多情 提交于 2019-12-02 06:20:53

问题


HTML:

<meta name="description" runat="server" id="MetaDescription" content="" />

Codebehind:

MetaDescription.Attributes["content"] = ThisBlog.MetaDescription;

This renders as:

<meta id="HeadContent_MetaDescription" name="description" content="My page description"></meta>

As per this answer it needs to have no ID attribute, and close with />.

How can I make it render in this way?


回答1:


I am still on .net 3.5 but put this in Page_Load and it will do what you need:

HtmlMeta keywords = new HtmlMeta();
keywords.Name = "keywords";
keywords.Content = "one two trhee;
Header.Controls.Add(keywords);

PS: the example is for the keywords tag but the result is the same.




回答2:


You should remove the id property from the <meta>, this means the control won't be accessible from the server side by id but instead of it it could be dynamically created and then added to the page:

HtmlMeta meta = new HtmlMeta();
meta.Name = "keywords";
meta.Content = ThisBlog.MetaDescription;
this.Header.Controls.Add(meta);


来源:https://stackoverflow.com/questions/5224995/asp-net-render-meta-tag-validly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!