C# 動態添加SEO 資訊,不和靜態頁面重複和疊加

來源:互聯網
上載者:User

  動態添加SEO 資訊,不和靜態頁面重複和疊加,就一個方法,用到了做個記錄,以後直接用就OK了,需要的同學也可以直接拿去用。

 

 1         /// <summary>
2 /// 動態設定 SEO 資訊
3 /// </summary>
4 /// <param name="title">標題</param>
5 /// <param name="key">關鍵字</param>
6 /// <param name="des">描述</param>
7 public void setMeta(string title, string key, string des)
8 {
9 if (!String.IsNullOrEmpty(title))
10 {
11 Page.Title += title;
12 }
13 if (!String.IsNullOrEmpty(key))
14 {
15 Boolean Edited = false;
16 for (int a_i = 0; a_i <= Page.Header.Controls.Count - 1; a_i++)
17 {
18 if (Page.Header.Controls[a_i].GetType() == typeof(HtmlMeta))
19 {
20 HtmlMeta SetMeta = (HtmlMeta)Page.Header.Controls[a_i];
21
22 if (SetMeta.Name.ToLower () == "keywords")
23 {
24 SetMeta.Content = SetMeta.Content.ToString() + " " + key;
25 Edited = true;
26 break;
27 }
28 }
29 }
30 if (!Edited)
31 {
32 HtmlMeta NewKeywords = new HtmlMeta();
33 NewKeywords.Name = "keywords";
34 NewKeywords.Content = key;
35 Page.Header.Controls.Add(NewKeywords);
36 }
37 }
38 if (!String.IsNullOrEmpty(des))
39 {
40 Boolean Edited = false;
41 for (int a_i = 0; a_i <= Page.Header.Controls.Count - 1; a_i++)
42 {
43 if (Page.Header.Controls[a_i].GetType() == typeof(HtmlMeta))
44 {
45 HtmlMeta SetMeta = (HtmlMeta)Page.Header.Controls[a_i];
46
47 if (SetMeta.Name.ToLower() == "description")
48 {
49 SetMeta.Content = SetMeta.Content.ToString() + " " + des;
50 Edited = true;
51 break;
52 }
53 }
54 }
55 if (!Edited)
56 {
57 HtmlMeta NewKeywords = new HtmlMeta();
58 NewKeywords.Name = "description";
59 NewKeywords.Content = des;
60 Page.Header.Controls.Add(NewKeywords);
61 }
62 }
63 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.