public static string ProcessOrderByOptions(this ODataV3QueryOptions rawOptions)
        {
            string orderby = null;

            if (!string.IsNullOrEmpty(rawOptions.OrderBy))
            {
                orderby = rawOptions.OrderBy.ToUpper() + ",";
                orderby = orderby.Replace(" ,", ",");
                orderby = orderby.Replace(" ASC,", "_ASC,");
                orderby = orderby.Replace(" DESC,", "_DESC,");
                orderby = orderby.Trim(new[] { ' ', ',' });

                var parts = orderby.Split(',');
                var list  = new List <string>();
                foreach (var part in parts)
                {
                    if (!(part.EndsWith("_ASC") || part.EndsWith("_DESC")))
                    {
                        list.Add(part + "_ASC");
                    }
                    else
                    {
                        list.Add(part);
                    }
                }
                orderby = string.Join(",", list.ToArray());
            }

            return(orderby);
        }
Ejemplo n.º 2
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext == null)
            {
                throw new ArgumentNullException(nameof(actionContext));
            }
            var request = actionContext.Request;

            if (request != null)
            {
                var raw = new ODataV3QueryOptions();

                if (request.Properties.ContainsKey("MS_QueryNameValuePairs"))
                {
                    try
                    {
                        var query = (KeyValuePair <string, string>[])request.Properties["MS_QueryNameValuePairs"];

                        foreach (var pair in query)
                        {
                            var name = pair.Key.ToLowerInvariant();
                            switch (name)
                            {
                            case "$top":
                                int.TryParse(pair.Value, NumberStyles.Integer, null, out var top);
                                raw.Top = top;
                                break;

                            case "$skip":
                                int.TryParse(pair.Value, NumberStyles.Integer, null, out var skip);
                                raw.Skip = skip;
                                break;

                            case "$inlinecount":
                                raw.InlineCount = string.Compare(pair.Value, "allpages", StringComparison.InvariantCultureIgnoreCase) == 0;
                                break;

                            case "$orderby":
                                raw.OrderBy = pair.Value;
                                break;

                            case "$filter":
                                raw.Filter = pair.Value;
                                break;

                            case "$expand":
                                raw.Expand = pair.Value;
                                break;

                            case "$format":
                                raw.Format = pair.Value;
                                break;
                            }
                        }

                        actionContext.ControllerContext.RouteData.Values.Add("options", raw);
                    }
                    catch (Exception ex)
                    {
                        Logger.HandleException(LoggingBoundaries.ServiceBoundary, ex);
                    }
                }

                else

                if (request.Properties.ContainsKey("MS_HttpContext"))
                {
                    if (request.Properties["MS_HttpContext"] is HttpContextBase httpContext)
                    {
                        var requestContext = httpContext.Request;

                        if (!string.IsNullOrEmpty(requestContext["$top"]))
                        {
                            int.TryParse(requestContext["$top"], NumberStyles.Integer, null, out var top);
                            raw.Top = top;
                        }

                        if (!string.IsNullOrEmpty(requestContext["$skip"]))
                        {
                            int.TryParse(requestContext["$skip"], NumberStyles.Integer, null, out int skip);
                            raw.Skip = skip;
                        }

                        raw.InlineCount = string.Compare(requestContext["$inlinecount"], "allpages",
                                                         StringComparison.InvariantCultureIgnoreCase) == 0;

                        raw.OrderBy = requestContext["$orderby"];
                        raw.Filter  = requestContext["$filter"];
                        raw.Expand  = requestContext["$expand"];
                        raw.Format  = requestContext["$format"];

                        actionContext.ControllerContext.RouteData.Values.Add("options", raw);
                    }
                }
            }

            base.OnActionExecuting(actionContext);
        }