在EPiServer 8 中,如何从内容项中获取ContentType?
In EPiServer 8, how can I get ContentType from a content item?
我需要检测 EPiServer 8.0 中 EPiServer 对象的内容类型。这是为了防止我们的代码遇到以下异常。
EPiServer.Core.TypeMismatchException: Content with id '202' is of type
'Castle.Proxies.PDFMediaFileProxy' which does not inherit required
type 'EPiServer.Core.PageData'
这里是一个简短的代码片段,显示了我们遇到异常的位置。
// This property in our class gets populated elswhere.
public List<IndexResponseItem> SearchResult { get; set; }
// Code in method that fails.
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
foreach (var item in SearchResult)
{
var foo = new UrlBuilder(item.GetExternalUrl());
IContent contentReference = UrlResolver.Current.Route(foo);
if (contentReference != null)
{
// This line of code breaks.
var currPage = repository.Get<PageData>(contentReference.ContentGuid);
}
}
以上代码在我们搜索 returns 任何 PageData 内容类型时有效。但如果它遇到 PDF 内容类型,就会中断。
获取 ContentTypeID 很简单(通过 contentReference.ContentTypeID
)。但我想实际检查每个对象的实际内容类型。我怎样才能得到内容类型?谢谢
MediaFile
对象不是 PageData
实例,因此您需要验证 contentReference is PageData
以及
if (contentReference != null && contentReference is PageData)
{
var currPage = repository.Get<PageData>(contentReference.ContentGuid);
}
不过,您似乎正在从 Episerver Search 构建自定义实现,我建议您查看文档中的示例 http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/8/Search/Search-integration/
==编辑==
要从内容项中解析内容类型,请使用 IContentTypeRepository
// content is an IContent object
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentType = contentTypeRepository.Load(content.ContentTypeID);
我需要检测 EPiServer 8.0 中 EPiServer 对象的内容类型。这是为了防止我们的代码遇到以下异常。
EPiServer.Core.TypeMismatchException: Content with id '202' is of type 'Castle.Proxies.PDFMediaFileProxy' which does not inherit required type 'EPiServer.Core.PageData'
这里是一个简短的代码片段,显示了我们遇到异常的位置。
// This property in our class gets populated elswhere.
public List<IndexResponseItem> SearchResult { get; set; }
// Code in method that fails.
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
foreach (var item in SearchResult)
{
var foo = new UrlBuilder(item.GetExternalUrl());
IContent contentReference = UrlResolver.Current.Route(foo);
if (contentReference != null)
{
// This line of code breaks.
var currPage = repository.Get<PageData>(contentReference.ContentGuid);
}
}
以上代码在我们搜索 returns 任何 PageData 内容类型时有效。但如果它遇到 PDF 内容类型,就会中断。
获取 ContentTypeID 很简单(通过 contentReference.ContentTypeID
)。但我想实际检查每个对象的实际内容类型。我怎样才能得到内容类型?谢谢
MediaFile
对象不是 PageData
实例,因此您需要验证 contentReference is PageData
以及
if (contentReference != null && contentReference is PageData)
{
var currPage = repository.Get<PageData>(contentReference.ContentGuid);
}
不过,您似乎正在从 Episerver Search 构建自定义实现,我建议您查看文档中的示例 http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/8/Search/Search-integration/
==编辑==
要从内容项中解析内容类型,请使用 IContentTypeRepository
// content is an IContent object
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentType = contentTypeRepository.Load(content.ContentTypeID);