在EventsPublishedContent期间更新ReadOnly属性

首先,我想在我的页面类型中添加一个只读类型属性,这可能吗? 我想要的是一个字符串属性,可以通过编程方式更新,但不能通过UI更新,我希望内容编辑器能够看到值但不能更改它。

假设可以在发布事件期间如何更新此属性值。

那我怎么……

private void EventsPublishedContent(object sender, ContentEventArgs e) { if (e.Content is MyPageType) { var myvalue = BussinessLogic.PerformAction(e.content) //Now I want to save myvalue on to a property in //e.content.myProperty } } 

请不要在stackoverflow上询问有关同一主题的多个问题

只读

使用[ReadOnly(true)][Editable(false)]注释创建只读属性。

活动

您正在寻找的是PublishingContent事件 。 有很多方法可以配置它

大多数人基本上需要在初始化模块中告诉Episerver您希望事件连接到特定IContent类型上的特定操作。

来自Episerver World

 [InitializableModule] [ModuleDependency(typeof(EPiServer.Web.InitializationModule))] public class ContentEventHandler : IInitializableModule { public void Initialize(InitializationEngine context) { var eventRegistry = ServiceLocator.Current.GetInstance(); eventRegistry.PublishingContent += OnPublishingContent; } private void OnPublishingContent(object sender, ContentEventArgs e) { if (e.Content.Name.Contains("BlockType")) { e.Content.Name = e.Content.Name.Replace("BlockType", "NewName"); } } } 

现在这是非常原始的,通常我实现了Alf Nilsson的EPiEventHelper 。 这样,您将获得实现事件处理的通用方法

来自https://talk.alfnilsson.se/2017/01/11/episerver-event-helper-v3-0/的片段。 您还可以在此处了解有关事件助手的更多信息

 [ServiceConfiguration(typeof(IPublishingContent))] public class PublishingStandardPage : PublishingContentBase { protected override void PublishingContent(object sender, TypedContentEventArgs e) { // Here you can access the standard page StandardPage standardPage = e.Content; } }