代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.NET
】
asp.net操作 META信息
作者:
/ 发布于
2016/7/28
/
566
public void setMeta(string key,string des) { HtmlMeta keywords = new HtmlMeta(), // keywords description = new HtmlMeta(); // description keywords.Name = "keywords"; keywords.Content = key; description.Name = "description"; description.Content = des; Page.Header.Controls.Add(keywords); Page.Header.Controls.Add(description); } 在ASP.NET编程中,由于经常采用一个页面通过不同的参数来显示不同的内容,因此常常需要实现动态输出不同的html header, 比如title, keywords, descrtptions等。 推荐的简单做法如下: 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); } 浏览时输出的页面源码便会达到如下效果: <head><title> This is a title and meta test page. </title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-cn" /><meta name="Description" content="Test the meta controls" /><<meta name="keywords" content="title,meta,test,page" /><link href="MasterPage.css" rel="stylesheet" type="text/css" /></head> 由于需要动态修改Header的Controls集合,因此如果放在用户自定义控件的Page_Onload事件中因为已经太迟而到处出现异常。对于希望用通用的控件实现这一功能的场合,建议包装一个普通的类,然后在MasterPager或者Page的Page_OnLoad中调用一下即可。 #region PAGE HEAD //Page meta information public void BackHeadContent(HtmlControl htmlCtrl) { Content(htmlCtrl, "网站后台管理系统", "~/CssStyle/SiteStyles.css"); } public void HeadContent(HtmlControl htmlCtrl, string strPageTitle) { Content(htmlCtrl, strPageTitle, "~/CssStyle/Styles.css"); } private void Content(HtmlControl htmlCtrl, string strTitle,string cssFile) { //Title HtmlTitle title = new HtmlTitle(); title.Text = strTitle; htmlCtrl.Controls.Add(title); //Link/CSSfile: HtmlLink cssLink = new HtmlLink(); cssLink.Href = cssFile; cssLink.Attributes.Add("rel", "stylesheet"); cssLink.Attributes.Add("type", "text/css"); htmlCtrl.Controls.Add(cssLink); HtmlMeta author = new HtmlMeta(), // author copyright = new HtmlMeta(), // copyright date = new HtmlMeta(), // date keywords = new HtmlMeta(), // keywords description = new HtmlMeta(), // description robots = new HtmlMeta();// robots author.Name = "Author"; author.Content = "Insus.NET"; copyright.Name = "Copyright"; copyright.Content = "Copyright 2008 Insus.NET"; date.Name = "date"; date.Content = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); keywords.Name = "keywords"; keywords.Content = "Insus "; description.Name = "description"; robots.Name = "robots"; robots.Content = "all"; string[] InsusWords = "VISUAL STUDIO 2003,VISUAL STUDIO 2005,Microsoft SQL Server 2005,ASP.NET,ASP.NET 2.0,VB.NET,C#,AJAX,LINQ" .Replace("\\r", string.Empty) .Replace("\\n", string.Empty) .Replace(">br />", string.Empty) .Replace(",", string.Empty) .Replace("\\'", string.Empty) .Split(' '); foreach (string word in InsusWords) keywords.Content += word + ","; if (keywords.Content.ToString().Length > 1024) { keywords.Content = keywords.Content.Substring(0, keywords.Content.IndexOf(" ", 1024)); } description.Content = "This web site use asp.net2.0 and C# and Ajax technology"; if (description.Content.ToString().Length > 1024) { description.Content = description.Content.Substring(0, description.Content.IndexOf(" ", 1024)); } htmlCtrl.Controls.Add(author); htmlCtrl.Controls.Add(copyright); htmlCtrl.Controls.Add(date); htmlCtrl.Controls.Add(keywords); htmlCtrl.Controls.Add(description); htmlCtrl.Controls.Add(robots); } #endregion PAGE HEAD
试试其它关键字
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3