我在尝试直接从页面获取pageMeta时遇到问题。以下是我的示例代码。如果我做错了,请你告诉我。
错误讯息: 指数超出范围。必须是非负数且小于集合的大小。 参数名称:IPageMeta上的索引pageMeta =(IPageMeta)结果[0];
public partial class ABCuserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Use PageMetaFactory and retrieve the metadata of the page located at /us/potential-at-work/application-leaders/article.aspx
string pubID = "tcm:0-12-1";
string url = HttpContext.Current.Request.Url.AbsoluteUri;
PageMetaFactory pageMetaFactory = new PageMetaFactory(pubID);
ComponentPresentationFactory cpFactory = new ComponentPresentationFactory(pubID);
ComponentMetaFactory compMetaFactory = new ComponentMetaFactory(pubID);
IList result = pageMetaFactory.GetMetaByUrl(url);
IPageMeta pageMeta = (IPageMeta)result[0];
//Get component from Page metadata
foreach (ComponentPresentationMeta cpmeta in pageMeta.ComponentPresentationMeta)
{
ComponentPresentation dcp = cpFactory.GetComponentPresentationWithHighestPriority(cpmeta.ComponentId); //PaW Article Detail CT was set with the High priority
//Get the custom metadata from the Article component in this CP
if (dcp != null)
{
IComponentMeta compMeta = compMetaFactory.GetMeta(dcp.ComponentId);
CustomMeta compCustomMeta = compMeta.CustomMeta;
if (compCustomMeta != null)
{
//Looping through to display the list of all custom meta fields
foreach (DictionaryEntry item in compCustomMeta.NameValues)
{
Response.Write(string.Format("{0}: {1} ({2})<br/>", item.Key, ((NameValuePair)item.Value).Value, ((NameValuePair)item.Value).Name));
}
//Get just one field
Response.Write(compCustomMeta.GetValue("edition").ToString());
}
}
}
}
}