是否可以在.NET 3.5中阅读Page MetaKeywords?

时间:2010-09-14 20:10:00

标签: c# asp.net .net-3.5

我需要阅读页面关键字,是否可以使用C#3.5?

2 个答案:

答案 0 :(得分:1)

string keywords = Page
    .Header
    .Controls
    .OfType<HtmlMeta>()
    .Where(meta => string.Equals(meta.Name, "keywords", StringComparison.OrdinalIgnoreCase))
    .Select(meta => meta.Content)
    .FirstOrDefault();

答案 1 :(得分:1)

试试这个:

// 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;