public override void OnActionExecuting(ActionExecutingContext context)
        {
            EnsureArg.IsNotNull(context, nameof(context));

            if (!context.HttpContext.Request.Headers.TryGetValue(HeaderNames.Accept, out var acceptHeaderValue) ||
                acceptHeaderValue.Count != 1 ||
                !string.Equals(acceptHeaderValue[0], KnownContentTypes.JsonContentType, StringComparison.OrdinalIgnoreCase))
            {
                throw new RequestNotValidException(string.Format(Resources.UnsupportedHeaderValue, HeaderNames.Accept));
            }

            if (!context.HttpContext.Request.Headers.TryGetValue(PreferHeaderName, out var preferHeaderValue) ||
                preferHeaderValue.Count != 1 ||
                !string.Equals(preferHeaderValue[0], PreferHeaderExpectedValue, StringComparison.OrdinalIgnoreCase))
            {
                throw new RequestNotValidException(string.Format(Resources.UnsupportedHeaderValue, PreferHeaderName));
            }

            // Validate that the request does not contain any query parameters.
            IQueryCollection queryCollection = context.HttpContext.Request.Query;

            if (queryCollection?.Count > 0)
            {
                string paramName = queryCollection.FirstOrDefault().Key;
                throw new RequestNotValidException(string.Format(Resources.UnsupportedParameter, paramName));
            }
        }
Пример #2
0
        private List <IFilterDescriptor> GetFilterData(IQueryCollection args)
        {
            IEnumerable <IFilterDescriptor> filters = new List <IFilterDescriptor>();
            var filterKeys = args.Where(o => o.Key.Contains("filterModel"))
                             .GroupBy(o => o.Key.Substring(o.Key.IndexOf("[") + 1, o.Key.IndexOf("]") - o.Key.IndexOf("[") - 1))
                             .Select(o => o.Key).ToArray();

            foreach (var key in filterKeys)
            {
                string filterType = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][filterType]")).Value.ToString();
                string type       = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][type]")).Value.ToString();
                var    conditions = args.Where(o => o.Key.Contains($"filterModel[{key}][condition")).GroupBy(o => o.Key.Substring(o.Key.IndexOf("[condition"), 12)).ToArray();
                if (conditions.Any())
                {
                    string oprt            = args.First(o => o.Key.Contains($"filterModel[{key}][operator]")).Value.ToString();
                    var    compositeFilter = new CompositeFilterDescriptor();
                    IEnumerable <IFilterDescriptor> filterSubItems = new FilterDescriptorCollection();
                    var maxConditionCount = Math.Min(conditions.Count(), 9);
                    for (var i = 0; i < maxConditionCount; i++)
                    {
                        string condetionFilterType = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][condition{i + 1}][filterType]")).Value.ToString();
                        string conditionType       = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][condition{i + 1}][type]")).Value.ToString();
                        string conditionFilter     = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][condition{i + 1}][filter]")).Value.ToString();
                        string conditionFilterTo   = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][condition{i + 1}][filterTo]")).Value.ToString();

                        //toto should be correct
                        filterSubItems = AddFilterDescriptor(filterSubItems, key, condetionFilterType, conditionType, conditionFilter, conditionFilterTo);
                    }
                    var collection = new FilterDescriptorCollection();
                    collection.AddRange(filterSubItems.ToList());
                    compositeFilter.FilterDescriptors = collection;
                    compositeFilter.LogicalOperator   = (oprt == "AND" ? FilterCompositionLogicalOperator.And : FilterCompositionLogicalOperator.Or);
                    filters = filters.Concat(new List <IFilterDescriptor>()
                    {
                        compositeFilter
                    });
                }
                else
                {
                    string filter   = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][filter]")).Value.ToString();
                    string filterTo = args.FirstOrDefault(o => o.Key.Contains($"filterModel[{key}][filterTo]")).Value.ToString();
                    filters = AddFilterDescriptor(filters, key, filterType, type, filter, filterTo);
                }
            }
            return(filters.ToList());
        }
        private string GetJsonpCallback(HttpRequest request)
        {
            string callback = string.Empty;

            if (request.Method == HttpMethod.Get.ToString())
            {
                IQueryCollection query = request.Query;

                if (query != null)
                {
                    KeyValuePair <string, StringValues> queryVal = query.FirstOrDefault(x => x.Key.ToLower() == "callback");

                    if (!queryVal.Equals(default(KeyValuePair <string, StringValues>)))
                    {
                        callback = queryVal.Value.FirstOrDefault();
                    }
                }
            }

            return(callback);
        }
Пример #4
0
        private static List <SortDescriptor> GetSortData(IQueryCollection args)
        {
            List <SortDescriptor> orderByItems = new List <SortDescriptor>();
            var s = args.Where(o => o.Key.Contains("sortModel"));
            int c = s.Count() / 2;

            for (var i = 0; i < c; i++)
            {
                string colId = args.FirstOrDefault(o => o.Key.Contains($"sortModel[{i}][colId]")).Value.ToString();
                string sort  = args.First(o => o.Key.Contains($"sortModel[{i}][sort]")).Value.ToString();
                if (!string.IsNullOrEmpty(colId) && !string.IsNullOrEmpty(sort))
                {
                    orderByItems.Add(new SortDescriptor()
                    {
                        Member = string.Concat(colId.Split(new[] { '-', '_' }, StringSplitOptions.RemoveEmptyEntries)
                                               .Select(word => word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower())),
                        SortDirection = sort.ToUpper() == "ASC" ? System.ComponentModel.ListSortDirection.Ascending : System.ComponentModel.ListSortDirection.Descending
                    });
                }
            }
            return(orderByItems);
        }
Пример #5
0
 public static string Extract(this IQueryCollection query, string key)
 {
     return(query.FirstOrDefault(x => x.Key == key).Value);
 }
Пример #6
0
 public static string GetStr(this IQueryCollection query, string key)
 {
     return(query.FirstOrDefault(x => x.Key == key).Value.FirstOrDefault());
 }