【问题标题】:Is it possible to read Page MetaKeywords in .NET 3.5?是否可以在 .NET 3.5 中读取页面元关键字?
【发布时间】:2010-09-14 20:10:00
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:
    string keywords = Page
        .Header
        .Controls
        .OfType<HtmlMeta>()
        .Where(meta => string.Equals(meta.Name, "keywords", StringComparison.OrdinalIgnoreCase))
        .Select(meta => meta.Content)
        .FirstOrDefault();
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        猜你喜欢
        • 2021-12-29
        • 2010-11-12
        • 1970-01-01
        • 2014-03-17
        • 2011-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-06
        相关资源
        最近更新 更多