コード例 #1
0
        private void PublishedContentHandler(object sender, ContentEventArgs e)
        {
            if (Configuration.Configuration.AddAutomaticRedirects == false)
            {
                return;
            }

            var lastVersion = ContentVersionRepository
                              .Service
                              .List(e.ContentLink)
                              .Where(p => p.Status == VersionStatus.PreviouslyPublished)
                              .OrderByDescending(p => p.Saved)
                              .FirstOrDefault();

            if (lastVersion == null)
            {
                return;
            }

            var oldUrl = GetContentUrl(lastVersion.ContentLink, lastVersion.LanguageBranch);

            if (oldUrl == null)
            {
                return;
            }

            var newUrl = GetContentUrl(e.ContentLink, e.Content.LanguageBranch());

            if (oldUrl == newUrl)
            {
                return;
            }

            var lastVersionPageData = ContentRepository.Service.Get <IContentData>(lastVersion.ContentLink) as PageData;

            if (lastVersionPageData == null)
            {
                return;
            }

            SystemRedirectsActions.AddRedirects(lastVersionPageData, oldUrl, SystemRedirectsHelper.GetCultureInfo(e), SystemRedirectReason.PublishedContent);
        }
コード例 #2
0
        private void SavedContentHandler(object sender, ContentEventArgs e)
        {
            if (Configuration.Configuration.AddAutomaticRedirects == false)
            {
                return;
            }

            var oldUrl = e.Items[OldUrlKey]?.ToString();

            if (oldUrl == null)
            {
                return;
            }

            var newUrl = UrlResolver.Service.GetUrl(e.ContentLink, null);

            if (newUrl != oldUrl)
            {
                var pageData = ContentRepository.Service.Get <IContentData>(e.ContentLink) as PageData;
                SystemRedirectsActions.AddRedirects(pageData, oldUrl, SystemRedirectsHelper.GetCultureInfo(e), SystemRedirectReason.SavedContent);
            }

            e.Items.Remove(OldUrlKey);
        }