asp.net动态设置标题title 关键字keywords 描述descrtptions
方法一
if (!IsPostBack)
{
//Page title网页标题
Page.Title = “我的网站标题”;//须将网页head标签设成服务器控件模式,即<head runat="server">,否则无效
//Description网页描述
HtmlMeta desc = new HtmlMeta();
desc.Name = "Description";
desc.Content = "我的网站描述";
Page.Header.Controls.Add(desc);
//Keyword网页关键字
HtmlMeta keywords = new HtmlMeta();
keywords.Name = "keywords";
keywords.Content = “我的网站关键字";
Page.Header.Controls.Add(keywords);
}
方法二<非母版页面下>
在网页的开发中为了搜索引擎的优化,添加页面的title keyword description是最常用的方法之一。asp.net的方法代码如下所示:
public void SEO_HEAD(string title, string keyword, string description)
{
Page.Title = title + "-my blog-";
HtmlMeta metaKeyWords = new HtmlMeta();
HtmlMeta metaDescription = new HtmlMeta();
metaKeyWords.Name = "Keywords";
metaKeyWords.Content = keyword;
metaDescription.Name = "description";
metaDescription.Content = description;
Header1.Controls.Add(metaKeyWords);
Header1.Controls.Add(metaDescription);//Header1是head的id
}
---------如果没有母版页的话把内容页的title标签删除----------
三 其他
推荐的简单做法如下:
protected void Page_Load(object sender, EventArgs e)
{
//Page title
Page.Title = "This is a title and meta test page.";
//Encode/Content type
HtmlMeta encode = new HtmlMeta();
encode.HttpEquiv = "Content-Type";
encode.Content = "text/html; charset=utf-8";
Page.Header.Controls.Add(encode);
//Language
HtmlMeta lang = new HtmlMeta();
lang.HttpEquiv = "Content-Language";
lang.Content = "zh-cn";
Page.Header.Controls.Add(lang);
//Description
HtmlMeta desc = new HtmlMeta();
desc.Name = "Description";
desc.Content = "Test the meta controls";
Page.Header.Controls.Add(desc);
//Keyword
HtmlMeta keywords = new HtmlMeta();
keywords.Name = "keywords";
keywords.Content = "title,meta,test,page";
Page.Header.Controls.Add(keywords);
//Link/CSS
HtmlLink cssLink = new HtmlLink();
cssLink.Href = "MasterPage.css";
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(cssLink);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。