【发布时间】:2010-09-14 20:10:00
【问题描述】:
我需要阅读页面关键字,用C# 3.5可以吗?
【问题讨论】:
我需要阅读页面关键字,用C# 3.5可以吗?
【问题讨论】:
string keywords = Page
.Header
.Controls
.OfType<HtmlMeta>()
.Where(meta => string.Equals(meta.Name, "keywords", StringComparison.OrdinalIgnoreCase))
.Select(meta => meta.Content)
.FirstOrDefault();
【讨论】:
试试这个:
// Store the keywords here:
List<String> keywords = new List<String>();
foreach (Control c in this.Page.Header.Controls)
{
HtmlMeta meta = c as HtmlMeta;
if (meta != null &&
String.Compare(meta.Name, "keywords", true, CultureInfo.InvariantCulture) == 0)
{
// When it is a Keywords meta tag, split the contents on each komma
// and trim the spaces off.
string[] kwds = (meta.Content.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
foreach(string s in kwds)
keywords.Add(s.Trim());
}
}
return keywords;
【讨论】: