动态设置网页的各种Meta

来源:互联网 发布:java接口自动化 编辑:程序博客网 时间:2024/04/29 11:26
如今网站的优化铺天盖地,其中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);