コード例 #1
0
        public override string GetControllerName(System.Net.Http.HttpRequestMessage request)
        {
            string controllerName = base.GetControllerName(request);
            int controllerVersion;
            if (request.Headers.Contains("X-Version"))
            {
                string headerValue = request.Headers.GetValues("X-Version").First();
                //If the X-Version is 0 then return the default version
                if (headerValue == "0") {
                    return controllerName;
                }

                //If the X-Version is 1 or 2 and if the ControllerName contains 'V or v' the return
                //the controller
                if (!String.IsNullOrEmpty(headerValue) && Int32.TryParse(headerValue, out controllerVersion))
                {
                    controllerName = String.Format("{0}v{1}", controllerName, controllerVersion);

                    HttpControllerDescriptor controllerDesc = null;

                    if (!this.GetControllerMapping().TryGetValue(controllerName, out controllerDesc))
                    {
                        string message = "No HTTP resource was found for specified request URI {0} and version {1}";
                        throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound, String.Format(message, request.RequestUri, controllerVersion)));
                    }
                }
            }
            return controllerName;
        }
コード例 #2
0
        public override void ValidateQuery(System.Net.Http.HttpRequestMessage request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }


            IEnumerable<KeyValuePair<string, string>> queryParameters = request.GetQueryNameValuePairs();
            foreach (KeyValuePair<string, string> kvp in queryParameters) {
                if (!ODataQueryOptions.IsSupported(kvp.Key) &&
                    !kvp.Key.Equals("$expand", StringComparison.InvariantCultureIgnoreCase) &&
                     kvp.Key.StartsWith("$", StringComparison.Ordinal)) {
                    // we don't allow any query parameters that starts with $ but we don't understand
                    throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.BadRequest,
                        string.Format("Query parameter {0} not supported", kvp.Key)));
                }
            }

        }