下面是代码:
public virtual IHtmlString Meta()
{
AggregateHtmlString htmlStrings = new AggregateHtmlString();
var htmlMeta = this.PageContext.HtmlMeta;
if (htmlMeta != null)
{
if (!string.IsNullOrEmpty(htmlMeta.Canonical))
{
htmlStrings.Add(new HtmlString(string.Format("<link rel=\"canonical\" href=\"{0}\"/>", Kooboo.StringExtensions.StripAllTags(htmlMeta.Canonical))));
}
if (!string.IsNullOrEmpty(htmlMeta.Author))
{
htmlStrings.Add(BuildMeta("author", htmlMeta.Author));
} if (!string.IsNullOrEmpty(htmlMeta.Description))
{
htmlStrings.Add(BuildMeta("description", htmlMeta.Description));
} if (!string.IsNullOrEmpty(htmlMeta.Keywords))
{
htmlStrings.Add(BuildMeta("keywords", htmlMeta.Keywords));
}
if (htmlMeta.Customs != null)
{
foreach (var item in htmlMeta.Customs)
{
htmlStrings.Add(BuildMeta(item.Key, item.Value));
}
}
if (!string.IsNullOrEmpty(htmlMeta.HtmlMetaBlock))
{
htmlStrings.Add(new HtmlString(htmlMeta.HtmlMetaBlock));
}
}
return htmlStrings;
}
private static IHtmlString BuildMeta(string name, string value)
{
return new HtmlString(string.Format("<meta name=\"{0}\" content=\"{1}\" />", name, Kooboo.StringExtensions.StripAllTags(value)));
}
其中的Aggregate的意思是聚合,是个生僻词。我还是觉得我们有必要了解一下AggregateHtmlString这个类吧。按F12进入定义,结果是DLL,搜索这个类名,得到如下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace Kooboo.CMS.Common
{
public class AggregateHtmlString : IHtmlString
{
List<IHtmlString> list;
public AggregateHtmlString()
: this(null)
{
}
public AggregateHtmlString(IEnumerable<IHtmlString> htmlStrings)
{
if (htmlStrings != null)
{
list = new List<IHtmlString>(htmlStrings);
}
else
{
list = new List<IHtmlString>();
}
}
public IEnumerable<IHtmlString> HtmlStrings
{
get
{
return list;
}
}
public void Add(IHtmlString htmlString)
{
list.Add(htmlString);
}
#region IHtmlString Members public string ToHtmlString()
{
StringBuilder sb = new StringBuilder();
foreach (var item in HtmlStrings)
{
if (item != null)
{
sb.Append(item.ToHtmlString());
}
}
return sb.ToString();
} #endregion
public override string ToString()
{
return ToHtmlString();
}
}
}
我们发现这个类其实是继承了IHtmlString接口,那边这个AggregateHtmlString是在这个方法内如何应用到的呢?
我们先注意到了这个东西: var htmlMeta = this.PageContext.HtmlMeta;
也就是说,HtmlMeta是一个类,我们来一探究竟:我们先按F12然后这里只有一个GET方法:
public HtmlMeta HtmlMeta
{
get
{
return htmlMeta;
}
}
然后我们再进入这个MODEL,具体的我已经注释了,当然除了看不懂意思的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace Kooboo.CMS.Sites.Models
{
[DataContract]
public class HtmlMeta
{
[DataMember(Order = )]
//作者
public string Author { get; set; }
[DataMember(Order = )]
//关键字
public string Keywords { get; set; }
[DataMember(Order = )]
//描述
public string Description { get; set; } [DataMember(Order = )]
public Dictionary<string, string> Customs
{
get;
set;
}
[DataMember(Order = )]
//html标题
public string HtmlTitle { get; set; } [DataMember(Order = )]
public string Canonical { get; set; }
[DataMember]
//html meat块
/// To allow write custom html meta block. for example: <meta http-equiv="charset" content="XXX"> <meta property="title" content="{title}">
public string HtmlMetaBlock { get; set; }
}
}
当某个“字段”不为空的时候,就调用BuildMeta方法,把IHtmlString添加到html的list当中去。其中第一个参数是键,表示是何种META(Keyword,description),有关于有哪些meta标签可以看这里。
private static IHtmlString BuildMeta(string name, string value)
{
return new HtmlString(string.Format("<meta name=\"{0}\" content=\"{1}\" />", name, Kooboo.StringExtensions.StripAllTags(value)));
}