コード例 #1
0
 public override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     if (!filterContext.IsChildAction)
     {
         CurrentVisitor.SetFootprints(filterContext.HttpContext);
     }
 }
コード例 #2
0
        public async Task SetProperties(FormDataCollection form)
        {
            IDictionary <string, object> values;

            if (form == null)
            {
                // Try as JSON.
                using (var stream = await Request.Content.ReadAsStreamAsync())
                {
                    stream.Seek(0, SeekOrigin.Begin);

                    using (var reader = new StreamReader(stream))
                    {
                        var json = await reader.ReadToEndAsync();

                        values = JsonConvert.DeserializeObject <IDictionary <string, object> >(json);
                    }
                }
            }
            else
            {
                values = form.ToDictionary(x => x.Key, x => (object)x.Value);
            }

            CurrentVisitor.SetProperties(values);
        }
コード例 #3
0
        public HttpResponseMessage AddToSegment(string segmentAlias)
        {
            if (string.IsNullOrEmpty(segmentAlias))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            CurrentVisitor.AddToSegment(segmentAlias);
            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
コード例 #4
0
        static T InternalGetSegmentedValue <T>(this IPublishedContent content, string propertyAlias, bool recursive = false, T defaultValue = default(T))
        {
            // Make sure the property have a segmented value
            if (content.HasSegmentedValue(propertyAlias, recursive: recursive))
            {
                // Fetch property
                var prop = content.GetProperty(propertyAlias, recursive);

                // Convert to our model
                var ncbtModel = prop.Value as NcbtValue;
                if (ncbtModel != null)
                {
                    // Find the active segment for the user
                    NcbtValueSegment segmentModel = null;
                    foreach (var segment in ncbtModel.Segments.OrderBy(s => s.SortOrder).Where(x => x.Alias != BehaviouralTargetingConstants.SegmentDefaultAlias))
                    {
                        // Check if user is in segment
                        if (CurrentVisitor.IsInSegment(segment.Alias))
                        {
                            // Match!
                            segmentModel = segment;
                            break;
                        }
                    }
                    // Fall back to default segment if none found
                    segmentModel = segmentModel ??
                                   ncbtModel.Segments.First(
                        x => x.Alias == BehaviouralTargetingConstants.SegmentDefaultAlias);

                    // Check if multivalue is possible
                    if (segmentModel.Value is JArray)
                    {
                        // We have a possible multivalue, get the prevalues
                        var preValueIter = umbraco.library.GetPreValues(ncbtModel.DataTypeId);
                        // Get value ids from segment model
                        var modelIds    = ((JArray)segmentModel.Value).Values <int>().ToList();
                        var modelValues = new List <object>();

                        // Go through prevalues
                        while (preValueIter.MoveNext())
                        {
                            // TODO: Check if multivalue view present in selected editor prevalue

                            // Get prevalues from xpath nav
                            var preValuesXpathNav = preValueIter.Current;
                            // Check for each prevalue id in the model
                            modelIds.ForEach(preValueId =>
                            {
                                // Try using xpath
                                var preValueValueXpathNav = preValuesXpathNav.Select("//preValue[@id='" + preValueId + "']");
                                if (preValueValueXpathNav.Count != 0)
                                {
                                    // We found our prevalue!
                                    preValueValueXpathNav.MoveNext();
                                    modelValues.Add(preValueValueXpathNav.Current.Value);
                                }
                            });
                        }

                        // Assign values back to model in CSV format, as Umbraco does
                        segmentModel.Value = string.Join(",", modelValues);
                    }

                    // Return value
                    return((T)segmentModel.Value);
                }

                // Not a segmented value, return if the type matches anyways
                if (prop.Value is T)
                {
                    return((T)prop.Value);
                }

                return(default(T));
            }

            // Not a segmented value, call Umbracos GetPropertyValue
            return(content.GetPropertyValue(propertyAlias, recursive, defaultValue));
        }
コード例 #5
0
 public bool IsInSegment(string segmentAlias)
 {
     return(CurrentVisitor.IsInSegment(segmentAlias));
 }
コード例 #6
0
        static void Main(string[] args)
        {
            try
            {
                var sList = new List <Student>()
                {
                    new StudentFree()
                    {
                        Id   = 1,
                        Name = "HeXin"
                    },
                    new StudentFree()
                    {
                        Id   = 2,
                        Name = "ZhangSan"
                    },
                    new StudentVIP()
                    {
                        Id   = 3,
                        Name = "GuoLi"
                    },
                    new StudentVIP()
                    {
                        Id   = 4,
                        Name = "JiHai"
                    }
                };

                {
                    foreach (var student in sList)
                    {
                        Console.WriteLine("--");
                        student.Study();
                    }
                }

                Console.WriteLine("*******************************");

                {
                    foreach (var student in sList)
                    {
                        Console.WriteLine("--");
                        student.Study();

                        //For Video
                        //if (student is StudentFree)
                        //{
                        //    Console.WriteLine($"{student.Name} get free video.");
                        //}
                        //else if (student is StudentVIP)
                        //{
                        //    Console.WriteLine($"{student.Name} get all video.");
                        //}
                        //else
                        //{
                        //    Console.WriteLine($"{student.Name} can't get video.");
                        //}

                        student.Video();
                    }
                }

                Console.WriteLine("*******************************");

                {
                    IVisitor visitor = new CurrentVisitor();
                    foreach (var student in sList)
                    {
                        Console.WriteLine("--");
                        student.Study();

                        //For Video
                        //if (student is StudentFree)
                        //{
                        //    Console.WriteLine($"{student.Name} get free video.");
                        //}
                        //else if (student is StudentVIP)
                        //{
                        //    Console.WriteLine($"{student.Name} get all video.");
                        //}
                        //else
                        //{
                        //    Console.WriteLine($"{student.Name} can't get video.");
                        //}

                        student.VideoVisitor(visitor);
                    }
                }
                {
                    IVisitor visitor = new PastVisitor();
                    foreach (var student in sList)
                    {
                        Console.WriteLine("--");
                        student.Study();

                        //For Video
                        //if (student is StudentFree)
                        //{
                        //    Console.WriteLine($"{student.Name} get free video.");
                        //}
                        //else if (student is StudentVIP)
                        //{
                        //    Console.WriteLine($"{student.Name} get all video.");
                        //}
                        //else
                        //{
                        //    Console.WriteLine($"{student.Name} can't get video.");
                        //}

                        student.VideoVisitor(visitor);
                    }
                }

                {
                    IVisitor visitor = new FutureVisitor();
                    foreach (var student in sList)
                    {
                        Console.WriteLine("--");
                        student.Study();

                        //For Video
                        //if (student is StudentFree)
                        //{
                        //    Console.WriteLine($"{student.Name} get free video.");
                        //}
                        //else if (student is StudentVIP)
                        //{
                        //    Console.WriteLine($"{student.Name} get all video.");
                        //}
                        //else
                        //{
                        //    Console.WriteLine($"{student.Name} can't get video.");
                        //}

                        student.VideoVisitor(visitor);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }