以编程方式确定Orchard中没有魔术字符串的内容项类型

时间:2012-11-13 19:10:56

标签: asp.net-mvc-3 orchardcms

我正在Orchard中实现一个自定义模块来跟踪每个内容项的视图数量。在我的处理程序中,我正在检查内容项的类型是否是默认的Orchard" Page"类型,但现在它使用Equals函数和魔术字符串。这是我的代码:

public class ContentItemViewRecordHandler : ContentHandler
{
    private readonly IOrchardServices services;
    private readonly IRepository<ContentItemViewRecord> repository;

    public ContentItemViewRecordHandler(IOrchardServices services, IRepository<ContentItemViewRecord> repository)
    {
        this.services = services;
        this.repository = repository;

        OnGetDisplayShape<CommonPart>(RecordView);
    }

    private void RecordView(BuildDisplayContext context, CommonPart part)
    {
        var contentItemType = context.ContentItem.ContentType;

        // THIS IS THE IF STATEMENT IN QUESTION
        if (!contentItemType.Equals("Page", StringComparison.CurrentCultureIgnoreCase))
        {
            return;
        }

        var contentItemViewRecord = new ContentItemViewRecord
        {
            ContentItemRecordId = context.ContentItem.Id,
            Username = services.WorkContext.CurrentUser.UserName,
            HostAddress = services.WorkContext.HttpContext.Request.UserHostAddress,
            DateCreated = DateTime.UtcNow
        };

        repository.Create(contentItemViewRecord);
    }
}

任何人都知道是否有办法确定没有魔术字符串的内容项的类型?

1 个答案:

答案 0 :(得分:2)

内容类型不是.NET类型。它们是仅在运行时存在的动态实体。因此,字符串可以很好地识别它们。