动态设置网页的各种Meta

动态设置网页的各种Meta

如今网站的优化铺天盖地,其中Meta的优化是必不可少的一步。其中每个页面都要有相关的meta信息,每个页面的都要去加上,但每个页面的meta不能相同,才对搜索引擎比较友好。那么每个页面就要动态的生成各种meta信息.
有两种方法可以实现要求:
(1)要加的meta都加个id 和 ruanat 作为服务器控件使用 <meta name="description" content="这个事网站的描述" id="description" runat="server" />
       在后台这样写 description.Content = "这个事网站的描述";
 (2)动态生成meta控件
        直接在后台写就行代码如下:
         HtmlHead head = (HtmlHead)Page.Header;

        HtmlMeta contentType = new HtmlMeta();//显示字符集的设定 设定页面使用的字符集
        contentType.HttpEquiv = "content-Type";
        contentType.Content = "text/html; charset=gb2312";
        head.Controls.Add(contentType);
这样就可以了。以下是我针对针对各个mate的代码,并一定每个页面都要写全,只写利于优化的几个就可以了
页面代码:
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="MetaTest_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%--<meta name="description" content="description" id="description" runat="server" />
<meta name="keywords" content="Keywords" id="Keywords" runat="server" />--%>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

 

后台代码:
   
        //description.Content = "这个事网站的描述";
        //Keywords.Content = "网站建设,网站优化";

        Page.Title = "各种Meta的写法";

        HtmlHead head = (HtmlHead)Page.Header;

        HtmlMeta contentType = new HtmlMeta();//显示字符集的设定 设定页面使用的字符集
        contentType.HttpEquiv = "content-Type";
        contentType.Content = "text/html; charset=gb2312";
        head.Controls.Add(contentType);

        HtmlMeta description = new HtmlMeta();//description用来告诉搜索引擎你的网站主要内容
        description.Name = "Description";
        description.Content = "描述该网站主要的功能和作用";
        head.Controls.Add(description);

        HtmlMeta Keywords = new HtmlMeta();//keywords用来告诉搜索引擎你网页的关键字是什么
        Keywords.Name = "Keywords";
        Keywords.Content = "这里写上网站的关键字注意每个关键字用英文的逗号隔开";
        head.Controls.Add(Keywords);

        HtmlMeta author = new HtmlMeta();//标注网页的作者
        author.Name = "Author";
        author.Content = "刘海全,446557021@qq.com";
        head.Controls.Add(author);

        HtmlMeta robots = new HtmlMeta();//机器人向导设置 robots用来告诉搜索机器人哪些页面需要索引,哪些页面不需要索引。content的参数有all,none,index,noindex,follow,nofollow。默认是all
        robots.Name = "Robots";
        robots.Content = "none";
        head.Controls.Add(robots);

        HtmlMeta Expires = new HtmlMeta();//设定网页的到期时间
        Expires.HttpEquiv = "Expires";
        Expires.Content = "Fri, 12 Jan 2001 18:18:18 GMT";
        head.Controls.Add(Expires);

        HtmlMeta Pragma = new HtmlMeta();//禁止浏览器从本地计算机的缓存中访问页面内容
        Pragma.HttpEquiv = "Pragma";
        Pragma.Content = "no-cache";
        head.Controls.Add(Pragma);

        HtmlMeta SetCookie = new HtmlMeta();//cookie设定 如果网页过期,那么存盘的cookie将被删除。必须使用GMT的时间格式
        SetCookie.HttpEquiv = "Set-Cookie";
        SetCookie.Content = "cookievalue=xxx; expires=Friday, 12-Jan-2001 18:18:18 GMT; path=/";
        head.Controls.Add(SetCookie);

        HtmlMeta Windowtarget = new HtmlMeta();//显示窗口的设定 强制页面在当前窗口以独立页面显示 用来防止别人在框架里调用自己的页面
        Windowtarget.HttpEquiv = "Window-target";
        Windowtarget.Content = "_top";
        head.Controls.Add(Windowtarget);

转载于:https://www.cnblogs.com/muou/archive/2010/05/10/1731727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值