如何在episerver cms中获取用户请求的页面的属性值

How to get the property value of the page which is requested by user in episerver cms

如何获取用户在 episerver cms 10 中请求的页面的 属性 值...

public string GetContent(string pageType, string propertyName)
{
    Type type = Type.GetType(pageType); //target type
    object o = Activator.CreateInstance(type);
    var pageLink = new ContentReference();
    var contentLoader= ServiceLocator.Current.GetInstance<IContentLoader>();
    var content = contentLoader.Get<type>(pageLink);
    var vals = content.GetPropertyValue(propertyName);
    return vals;
}

在上面的方法中,我从 url 中获取了页面名称和 属性 名称.... 所以在这里我已经将变量 pageType (即页面名称)转换为 class 并在 Get<> 方法中使用它..但它不起作用......请有人告诉我解决方案...... 或者有没有其他方法可以在请求的页面中找到用户请求 属性 的 属性 vakue.....

我认为您误解了一些核心概念。

您应该执行如下操作:

// Get object used to load Episerver content
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

// Some content reference
var contentLink = new ContentReference(123);

// Get content of a specific type
var somePage = contentLoader.Get<SomePageType>(contentLink);

// Strongly typed access to content property
var somePropertyValue = somePage.SomeProperty;

如果您确实必须通过其属性名称获取值:

var someOtherProperty = somePage.GetPropertyValue("SomeOtherPropertyName");

这个问题的答案是:

public string GetContent(string pageName, string propertyName)
    {
        string content = string.Empty;
        try
        {
            log.Info("GetContent Method is called for getting property value!!!!!");
            IContentTypeRepository contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
            IEnumerable<ContentType> allPageTypes = contentTypeRepository.List().OfType<PageType>();
            IContentModelUsage contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
            IList<ContentUsage> pageInstanceCollection = new List<ContentUsage>();
            foreach (ContentType item in allPageTypes)
            {
                IList<ContentUsage> pageInstance = contentModelUsage.ListContentOfContentType(item);
                foreach (ContentUsage i in pageInstance)
                {
                    pageInstanceCollection.Add(i);
                }
            }
            IEnumerable<ContentUsage> currentpage = pageInstanceCollection.Where(x => x.Name.ToLower() == pageName.ToLower());
            int Id = currentpage.First().ContentLink.ID;
            PageReference pagereference = new PageReference(Id);
            IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            PageData pageData = contentRepository.Get<PageData>(pagereference);
            content = pageData.GetPropertyValue(propertyName);

        }
        catch(Exception exception)
        {
            string errorMessage = string.Format("Error in Content Retrieval : {0}", exception.Message);
            log.Error(errorMessage, exception);
        }
        return content;
    }

这里我将 CMS 页面名称和 属性 名称传递给方法以获取相应的 属性 值..