Пример #1
0
        public void CanSaveAndGetWithCulture()
        {
            string culture = "en";

            using (IScope scope = ScopeProvider.CreateScope())
            {
                IRedirectUrlRepository repo = CreateRepository(ScopeProvider);
                var rurl = new RedirectUrl
                {
                    ContentKey = _textpage.Key,
                    Url        = "blah",
                    Culture    = culture
                };
                repo.Save(rurl);
                scope.Complete();

                Assert.AreNotEqual(0, rurl.Id);
            }

            using (IScope scope = ScopeProvider.CreateScope())
            {
                IRedirectUrlRepository repo = CreateRepository(ScopeProvider);
                IRedirectUrl           rurl = repo.GetMostRecentUrl("blah");
                scope.Complete();

                Assert.IsNotNull(rurl);
                Assert.AreEqual(_textpage.Id, rurl.ContentId);
                Assert.AreEqual(culture, rurl.Culture);
            }
        }
Пример #2
0
        protected virtual bool HandleUmbracoRedirect(SpaApiRequest request, out HttpResponseMessage response)
        {
            response = null;

            // Get a reference to Umbraco's redirect URL service
            IRedirectUrlService service = UmbracoContext.Application.Services.RedirectUrlService;

            // Look for a matching redirect
            IRedirectUrl umbRedirect = service.GetMostRecentRedirectUrl(request.SiteId + request.Url.TrimEnd('/'));

            if (umbRedirect == null)
            {
                return(false);
            }

            // Get the destination page from the content cache
            IPublishedContent newContent = UmbracoContext.ContentCache.GetById(umbRedirect.ContentId);

            if (newContent == null)
            {
                return(false);
            }

            // Send a redirect response if a page was found
            response = ReturnRedirect(request, newContent.Url, true);
            return(true);
        }
Пример #3
0
        public void CanSaveAndGet()
        {
            IScopeProvider provider = ScopeProvider;

            using (IScope scope = provider.CreateScope())
            {
                IRedirectUrlRepository repo = CreateRepository(provider);
                var rurl = new RedirectUrl
                {
                    ContentKey = _textpage.Key,
                    Url        = "blah"
                };
                repo.Save(rurl);
                scope.Complete();

                Assert.AreNotEqual(0, rurl.Id);
            }

            using (IScope scope = provider.CreateScope())
            {
                IRedirectUrlRepository repo = CreateRepository(provider);
                IRedirectUrl           rurl = repo.GetMostRecentUrl("blah");
                scope.Complete();

                Assert.IsNotNull(rurl);
                Assert.AreEqual(_textpage.Id, rurl.ContentId);
            }
        }
Пример #4
0
 public void Delete(IRedirectUrl redirectUrl)
 {
     using (var scope = ScopeProvider.CreateScope())
     {
         _redirectUrlRepository.Delete(redirectUrl);
         scope.Complete();
     }
 }
 // Umbraco.Code.MapAll
 private void Map(IRedirectUrl source, ContentRedirectUrl target, MapperContext context)
 {
     target.ContentId      = source.ContentId;
     target.CreateDateUtc  = source.CreateDateUtc;
     target.Culture        = source.Culture;
     target.DestinationUrl = source.ContentId > 0 ? _publishedUrlProvider?.GetUrl(source.ContentId, culture: source.Culture) : "#";
     target.OriginalUrl    = _publishedUrlProvider?.GetUrlFromRoute(source.ContentId, source.Url, source.Culture);
     target.RedirectId     = source.Key;
 }
Пример #6
0
 public void Delete(IRedirectUrl redirectUrl)
 {
     using (var uow = UowProvider.GetUnitOfWork())
     {
         var repo = RepositoryFactory.CreateRedirectUrlRepository(uow);
         repo.Delete(redirectUrl);
         uow.Commit();
     }
 }
        /// <summary>
        /// Tries to find and assign an Umbraco document to a <c>PublishedRequest</c>.
        /// </summary>
        /// <param name="frequest">The <c>PublishedRequest</c>.</param>
        /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
        /// <remarks>Optionally, can also assign the template or anything else on the document request, although that is not required.</remarks>
        public bool TryFindContent(IPublishedRequestBuilder frequest)
        {
            if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
            {
                return(false);
            }

            var route = frequest.Domain != null
                ? frequest.Domain.ContentId + DomainUtilities.PathRelativeToDomain(frequest.Domain.Uri, frequest.AbsolutePathDecoded)
                : frequest.AbsolutePathDecoded;

            IRedirectUrl redirectUrl = _redirectUrlService.GetMostRecentRedirectUrl(route, frequest.Culture);

            if (redirectUrl == null)
            {
                _logger.LogDebug("No match for route: {Route}", route);
                return(false);
            }

            IPublishedContent content = umbracoContext.Content.GetById(redirectUrl.ContentId);
            var url = content == null ? "#" : content.Url(_publishedUrlProvider, redirectUrl.Culture);

            if (url.StartsWith("#"))
            {
                _logger.LogDebug("Route {Route} matches content {ContentId} which has no URL.", route, redirectUrl.ContentId);
                return(false);
            }

            // Appending any querystring from the incoming request to the redirect URL
            url = string.IsNullOrEmpty(frequest.Uri.Query) ? url : url + frequest.Uri.Query;

            _logger.LogDebug("Route {Route} matches content {ContentId} with URL '{Url}', redirecting.", route, content.Id, url);

            frequest
            .SetRedirectPermanent(url)

            // From: http://stackoverflow.com/a/22468386/5018
            // See http://issues.umbraco.org/issue/U4-8361#comment=67-30532
            // Setting automatic 301 redirects to not be cached because browsers cache these very aggressively which then leads
            // to problems if you rename a page back to it's original name or create a new page with the original name
            .SetNoCacheHeader(true)
            .SetCacheExtensions(new List <string> {
                "no-store, must-revalidate"
            })
            .SetHeaders(new Dictionary <string, string> {
                { "Pragma", "no-cache" }, { "Expires", "0" }
            });

            return(true);
        }
Пример #8
0
        public void CanSaveAndGetMostRecentForCulture()
        {
            string cultureA = "en";
            string cultureB = "de";

            Assert.AreNotEqual(_textpage.Id, _otherpage.Id);

            using (IScope scope = ScopeProvider.CreateScope())
            {
                IRedirectUrlRepository repo = CreateRepository(ScopeProvider);
                var rurl = new RedirectUrl
                {
                    ContentKey = _textpage.Key,
                    Url        = "blah",
                    Culture    = cultureA
                };
                repo.Save(rurl);
                scope.Complete();

                Assert.AreNotEqual(0, rurl.Id);

                // FIXME: too fast = same date = key violation?
                // and... can that happen in real life?
                // we don't really *care* about the IX, only supposed to make things faster...
                // BUT in realife we AddOrUpdate in a trx so it should be safe, always
                rurl = new RedirectUrl
                {
                    ContentKey    = _otherpage.Key,
                    Url           = "blah",
                    CreateDateUtc = rurl.CreateDateUtc.AddSeconds(1), // ensure time difference
                    Culture       = cultureB
                };
                repo.Save(rurl);
                scope.Complete();

                Assert.AreNotEqual(0, rurl.Id);
            }

            using (IScope scope = ScopeProvider.CreateScope())
            {
                IRedirectUrlRepository repo = CreateRepository(ScopeProvider);
                IRedirectUrl           rurl = repo.GetMostRecentUrl("blah", cultureA);
                scope.Complete();

                Assert.IsNotNull(rurl);
                Assert.AreEqual(_textpage.Id, rurl.ContentId);
                Assert.AreEqual(cultureA, rurl.Culture);
            }
        }
Пример #9
0
        /// <summary>
        /// Virtual method for handling redirects created automatically by Umbraco when editors rename and move content. The redirects handled by this method are the ones retrived through the <see cref="IRedirectUrlService"/> service.
        /// </summary>
        /// <param name="request">The current request.</param>
        /// <returns><c>true</c> if a redirect was found, otherwise <c>false</c>.</returns>
        protected virtual bool HandleUmbracoRedirect(SpaRequest request)
        {
            // Look for a matching redirect
            IRedirectUrl umbRedirect = Services.RedirectUrlService.GetMostRecentRedirectUrl(request.SiteId + request.Url.TrimEnd('/'));

            if (umbRedirect == null)
            {
                return(false);
            }

            // Get the destination page from the content cache
            IPublishedContent newContent = UmbracoContext.Content.GetById(umbRedirect.ContentId);

            if (newContent == null)
            {
                return(false);
            }

            // Send a redirect response if a page was found
            request.Response = ReturnRedirect(request, newContent.Url, true);
            return(true);
        }
Пример #10
0
        public void Can_Get_Most_Recent_RedirectUrl()
        {
            IRedirectUrl redirect = RedirectUrlService.GetMostRecentRedirectUrl(Url);

            Assert.AreEqual(redirect.ContentId, _secondSubPage.Id);
        }
Пример #11
0
 private static string GetUrl(IUmbracoContextAccessor umbracoContextAccessor, IRedirectUrl item) => umbracoContextAccessor?.UmbracoContext?.UrlProvider?.GetUrl(item.ContentId, item.Culture);