動態添加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 }