艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

自动设置umbracoUrlName为当前Id

UmbracoCMS在发布文章内容后,自动设置umbracoUrlName为当前Id(umbracoUrlName是Umbraco内置的属性,用于自定义URL地址)

新建MyEventHandler.cs文件,放在网站根目录的App_Code下即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core;

public class MyEventHandler : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        Umbraco.Core.Services.ContentService.Published += ContentService_Published;
    }
    void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs e)
    {
        var model = e.PublishedEntities.First();
        if (model.ContentType.Name.ToLower().Equals("article"))
        {
            Umbraco.Core.Services.ContentService.Published -= ContentService_Published;
            model.SetValue("umbracoUrlName", model.Id);
            ApplicationContext.Current.Services.ContentService.Publish(model);
            Umbraco.Core.Services.ContentService.Published += ContentService_Published;
        }
    }
}