コード例 #1
0
		public override void Initialize()
		{
			base.Initialize();

			var url = "/test";
			
			var lookup = new Umbraco.Web.Routing.ContentFinderByNiceUrl();
			var lookups = new Umbraco.Web.Routing.IContentFinder[] { lookup };

			var t = Template.MakeNew("test", new User(0));

			var umbracoContext = GetUmbracoContext(url, t.Id);
            var urlProvider = new UrlProvider(umbracoContext, new IUrlProvider[] { new DefaultUrlProvider() });
			var routingContext = new RoutingContext(
				umbracoContext,
				lookups,
				new FakeLastChanceFinder(),
                urlProvider);

			//assign the routing context back to the umbraco context
			umbracoContext.RoutingContext = routingContext;

			////assign the routing context back to the umbraco context
			//umbracoContext.RoutingContext = routingContext;
			Umbraco.Web.UmbracoContext.Current = routingContext.UmbracoContext;
		}
コード例 #2
0
        public TagsOrCategoryPathRouteConstraint(UrlProvider umbracoUrlProvider, IEnumerable<IPublishedContent> itemsForRoute)
        {
            if (itemsForRoute == null) throw new ArgumentNullException(nameof(itemsForRoute));

            foreach (var node in itemsForRoute)
            {
                var allUrls = ArticulateRoutes.GetContentUrls(umbracoUrlProvider, node);

                foreach (var url in allUrls)
                {
                    //if there is a double slash, it will have a domain
                    if (url.Contains("//"))
                    {
                        var uri = new Uri(url, UriKind.Absolute);
                        _urlNames.Add(new UrlNames
                        {
                            Host = uri.Host,
                            CategoryUrlName = node.GetPropertyValue<string>("categoriesUrlName"),
                            TagsUrlName = node.GetPropertyValue<string>("tagsUrlName")
                        });
                    }
                    else
                    {
                        _urlNames.Add(new UrlNames
                        {
                            Host = string.Empty,
                            CategoryUrlName = node.GetPropertyValue<string>("categoriesUrlName"),
                            TagsUrlName = node.GetPropertyValue<string>("tagsUrlName")
                        });
                    }
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RoutingContext"/> class.
 /// </summary>
 /// <param name="contentFinders">The document lookups resolver.</param>
 /// <param name="contentLastChanceFinder"> </param>
 /// <param name="urlProvider">The nice urls provider.</param>
 internal RoutingContext(
     IEnumerable<IContentFinder> contentFinders,
     IContentFinder contentLastChanceFinder,
     UrlProvider urlProvider)
 {
     _publishedContentFinders = new Lazy<IEnumerable<IContentFinder>>(() => contentFinders, false);
     _publishedContentLastChanceFinder = new Lazy<IContentFinder>(() => contentLastChanceFinder, false);
     _urlProvider = new Lazy<UrlProvider>(() => urlProvider, false);
 }
コード例 #4
0
	    /// <summary>
	    /// Initializes a new instance of the <see cref="RoutingContext"/> class.
	    /// </summary>
	    /// <param name="umbracoContext"> </param>
	    /// <param name="contentFinders">The document lookups resolver.</param>
	    /// <param name="contentLastChanceFinder"> </param>
	    /// <param name="urlProvider">The nice urls provider.</param>
	    internal RoutingContext(
			UmbracoContext umbracoContext,
			IEnumerable<IContentFinder> contentFinders,
			IContentFinder contentLastChanceFinder,
            UrlProvider urlProvider)
        {
			UmbracoContext = umbracoContext;
			PublishedContentFinders = contentFinders;
			PublishedContentLastChanceFinder = contentLastChanceFinder;
        	UrlProvider = urlProvider;
        }
コード例 #5
0
 public ArticulateSearchRouteHandler(UrlProvider umbracoUrlProvider, IEnumerable<IPublishedContent> itemsForRoute)
     : base(umbracoUrlProvider, itemsForRoute)
 {
     foreach (var node in itemsForRoute)
     {
         _urlNames.Add(new UrlNames
         {
             NodeId = node.Id,
             SearchUrlName = node.GetPropertyValue<string>("searchUrlName"),
             SearchPageName = node.GetPropertyValue<string>("searchPageName")
         });
     }
 }
コード例 #6
0
 /// <summary>
 /// Constructor used to create a new handler for multi-tenency with domains and ids
 /// </summary>
 /// <param name="itemsForRoute"></param>
 public ArticulateTagsRouteHandler(UrlProvider umbracoUrlProvider, IEnumerable<IPublishedContent> itemsForRoute)
     : base(umbracoUrlProvider, itemsForRoute)
 {
     foreach (var node in itemsForRoute)
     {
         _urlsAndPageNames.Add(new UrlAndPageNames
         {
             NodeId = node.Id,
             TagsUrlName = node.GetPropertyValue<string>("tagsUrlName"),
             TagsPageName = node.GetPropertyValue<string>("tagsPageName"),
             CategoriesUrlName = node.GetPropertyValue<string>("categoriesUrlName"),
             CategoriesPageName = node.GetPropertyValue<string>("categoriesPageName")
         });
     }
 }
コード例 #7
0
        public async void RouteUmbracoContentAsync_Umbraco_Context_Initialized()
        {
            var router = new UmbracoRouter(Mock.Of<IRouter>());
            var httpCtxAccessor = new Mock<IHttpContextAccessor>();
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(context => context.Request).Returns(Mock.Of<HttpRequest>());
            httpCtxAccessor.Setup(accessor => accessor.HttpContext).Returns(httpContext.Object);
            var umbCtx = new UmbracoContext(httpCtxAccessor.Object);
            var urlProvider = new UrlProvider(umbCtx, Enumerable.Empty<IUrlProvider>());
            var routingCtx = new RoutingContext(Enumerable.Empty<IContentFinder>(), Mock.Of<ILastChanceContentFinder>(), urlProvider);
            var pcr = new PublishedContentRequest(routingCtx, Mock.Of<ITemplateService>(), Mock.Of<ILoggerFactory>(), httpCtxAccessor.Object);

            var result = await router.RouteUmbracoContentAsync(umbCtx, pcr, new RouteData());

            Assert.Equal(true, umbCtx.Initialized);
            Assert.Equal(false, result);
        }
コード例 #8
0
	    ///  <summary>
	    ///  Return a new RoutingContext
	    ///  </summary>
	    ///  <param name="url"></param>
	    ///  <param name="templateId">
	    ///  The template Id to insert into the Xml cache file for each node, this is helpful for unit testing with templates but you		 
	    ///  should normally create the template in the database with this id
	    /// </param>
	    ///  <param name="routeData"></param>
	    /// <param name="setUmbracoContextCurrent">set to true to also set the singleton UmbracoContext.Current to the context created with this method</param>
	    /// <returns></returns>
	    protected RoutingContext GetRoutingContext(string url, int templateId, RouteData routeData = null, bool setUmbracoContextCurrent = false)
		{
			var umbracoContext = GetUmbracoContext(url, templateId, routeData);
            var urlProvider = new UrlProvider(umbracoContext, new IUrlProvider[] { new DefaultUrlProvider() });
			var routingContext = new RoutingContext(
				umbracoContext,
				Enumerable.Empty<IContentFinder>(),
				new FakeLastChanceFinder(),
                urlProvider);

			//assign the routing context back to the umbraco context
			umbracoContext.RoutingContext = routingContext;

	        if (setUmbracoContextCurrent)
	            UmbracoContext.Current = umbracoContext;

			return routingContext;
		}
コード例 #9
0
        /// <summary>
        /// Constructor used to create a new handler for multi-tenency with domains and ids
        /// </summary>
        /// <param name="umbracoUrlProvider"></param>
        /// <param name="itemsForRoute"></param>
        public ArticulateVirtualNodeByIdRouteHandler(UrlProvider umbracoUrlProvider, IEnumerable<IPublishedContent> itemsForRoute)
        {
            foreach (var publishedContent in itemsForRoute)
            {
                var allUrls = ArticulateRoutes.GetContentUrls(umbracoUrlProvider, publishedContent);

                foreach (var url in allUrls)
                {
                    //if there is a double slash, it will have a domain
                    if (url.Contains("//"))
                    {
                        var uri = new Uri(url, UriKind.Absolute);
                        _hostsAndIds.Add(new Tuple<string, int>(uri.Host, publishedContent.Id));
                    }
                    else
                    {
                        _hostsAndIds.Add(new Tuple<string, int>(string.Empty, publishedContent.Id));
                    }
                }
                LogHelper.Debug<ArticulateVirtualNodeByIdRouteHandler>(() => $"Hosts/IDs map for node {publishedContent.Id}. Values: {DebugHostIdsCollection()}");
            }
        }
コード例 #10
0
        public void GetUmbracoRouteValues_Find_Custom_Controller()
        {
            var router = new UmbracoRouter(Mock.Of<IRouter>());
            var httpCtxAccessor = new Mock<IHttpContextAccessor>();
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(context => context.Request).Returns(Mock.Of<HttpRequest>());
            httpCtxAccessor.Setup(accessor => accessor.HttpContext).Returns(httpContext.Object);
            var umbCtx = new UmbracoContext(httpCtxAccessor.Object);
            var urlProvider = new UrlProvider(umbCtx, Enumerable.Empty<IUrlProvider>());
            var routingCtx = new RoutingContext(Enumerable.Empty<IContentFinder>(), Mock.Of<ILastChanceContentFinder>(), urlProvider);
            var templateService = new Mock<ITemplateService>();
            templateService.Setup(service => service.GetTemplate("Hello")).Returns(Mock.Of<ITemplate>(template => template.Alias == "Hello"));
            var pcr = new PublishedContentRequest(routingCtx, templateService.Object, Mock.Of<ILoggerFactory>(), httpCtxAccessor.Object)
            {
                PublishedContent = new PublishedContent()
                {
                    ContentType = "Custom"
                }
            };
            pcr.TrySetTemplate("Hello");
            umbCtx.Initialize(pcr);
            var actionDescriptors = new Mock<IActionDescriptorsCollectionProvider>();
            actionDescriptors.Setup(provider => provider.ActionDescriptors).Returns(new ActionDescriptorsCollection(
                new List<ActionDescriptor>()
                {
                    new ControllerActionDescriptor()
                    {
                        Name = "Hello",
                        ControllerName = "Custom",
                        ControllerTypeInfo = typeof(UmbracoController).GetTypeInfo()
                    }
                }, 0));

            var result = router.GetUmbracoRouteValues(umbCtx, new UmbracoControllerTypeCollection(actionDescriptors.Object));

            Assert.Equal("Custom", result.ControllerName);
            Assert.Equal("Hello", result.ActionName);
        }
コード例 #11
0
        public void GetUmbracoRouteValues_Returns_Default()
        {
            var router = new UmbracoRouter(Mock.Of<IRouter>());
            var httpCtxAccessor = new Mock<IHttpContextAccessor>();
            var httpContext = new Mock<HttpContext>();
            httpContext.Setup(context => context.Request).Returns(Mock.Of<HttpRequest>());
            httpCtxAccessor.Setup(accessor => accessor.HttpContext).Returns(httpContext.Object);
            var umbCtx = new UmbracoContext(httpCtxAccessor.Object);            
            var urlProvider = new UrlProvider(umbCtx, Enumerable.Empty<IUrlProvider>());
            var routingCtx = new RoutingContext(Enumerable.Empty<IContentFinder>(), Mock.Of<ILastChanceContentFinder>(), urlProvider);
            var pcr = new PublishedContentRequest(routingCtx, Mock.Of<ITemplateService>(), Mock.Of<ILoggerFactory>(), httpCtxAccessor.Object)
            {
                PublishedContent = new PublishedContent()
            };
            umbCtx.Initialize(pcr);
            var actionDescriptors = new Mock<IActionDescriptorsCollectionProvider>();
            actionDescriptors.Setup(provider => provider.ActionDescriptors).Returns(new ActionDescriptorsCollection(new List<ActionDescriptor>(), 0));

            var result = router.GetUmbracoRouteValues(umbCtx, new UmbracoControllerTypeCollection(actionDescriptors.Object));

            Assert.Equal("Umbraco", result.ControllerName);
            Assert.Equal("Index", result.ActionName);
        }
コード例 #12
0
        /// <summary>
        /// This is a helper method which is called to ensure that the singleton context is created and the nice url and routing
        /// context is created and assigned.
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="applicationContext"></param>
        /// <param name="webSecurity"></param>
        /// <param name="replaceContext">
        /// if set to true will replace the current singleton with a new one, this is generally only ever used because
        /// during application startup the base url domain will not be available so after app startup we'll replace the current
        /// context with a new one in which we can access the httpcontext.Request object.
        /// </param>
        /// <param name="preview"></param>
        /// <returns>
        /// The Singleton context object
        /// </returns>
        /// <remarks>
        /// This is created in order to standardize the creation of the singleton. Normally it is created during a request
        /// in the UmbracoModule, however this module does not execute during application startup so we need to ensure it
        /// during the startup process as well.
        /// See: http://issues.umbraco.org/issue/U4-1890, http://issues.umbraco.org/issue/U4-1717
        /// </remarks>
        public static UmbracoContext EnsureContext(
            HttpContextBase httpContext,
            ApplicationContext applicationContext,
            WebSecurity webSecurity,
            bool replaceContext,
            bool? preview)
        {
            if (UmbracoContext.Current != null)
            {
                if (!replaceContext)
                    return UmbracoContext.Current;
                UmbracoContext.Current._replacing = true;
            }

            var umbracoContext = new UmbracoContext(
                httpContext,
                applicationContext,
                PublishedCachesResolver.Current.Caches,
                webSecurity,
                preview);

            // create the nice urls provider
            // there's one per request because there are some behavior parameters that can be changed
            var urlProvider = new UrlProvider(
                umbracoContext,
                UrlProviderResolver.Current.Providers);

            // create the RoutingContext, and assign
            var routingContext = new RoutingContext(
                umbracoContext,
                ContentFinderResolver.Current.Finders,
                ContentLastChanceFinderResolver.Current.Finder,
                urlProvider);

            //assign the routing context back
            umbracoContext.RoutingContext = routingContext;

            //assign the singleton
            UmbracoContext.Current = umbracoContext;
            return UmbracoContext.Current;
        }