public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            //TODO: See http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
            if (bindingContext.ModelType != typeof(ApiRequest))
            {
                return(false);
            }

            ApiRequest result = new ApiRequest
            {
                Options = ApiUtility.BuildRepositorySettings(actionContext.RequestContext, actionContext.Request)
            };

            bindingContext.Model = result;

            if (actionContext.Request.Headers.Accept != null)
            {
                result.Accept = actionContext.Request.Headers.Accept.ToList();
            }

            if (actionContext.Request.Content.Headers.ContentLength > 0)
            {
                result.BodyType = actionContext.Request.Content.Headers.ContentType;
                result.Body     = actionContext.Request.Content.ReadAsByteArrayAsync().Result;

                return(true);
            }

            var auth = actionContext.Request.Headers.Authorization;

            if (auth != null)
            {
                result.Auth = string.Format("{0}|{1}", auth.Scheme, auth.Parameter);
            }

            if (bindingContext.ValueProvider.ContainsPrefix("id"))
            {
                result.HasKey = true;
                result.Id     = bindingContext.ValueProvider.GetValue("id").RawValue as string;
                return(true);
            }

            if (bindingContext.ValueProvider.ContainsPrefix("RefType"))
            {
                result.HasReference = true;
                result.RefType      = bindingContext.ValueProvider.GetValue("RefType").RawValue as string;
                result.RefValue     = bindingContext.ValueProvider.GetValue("RefValue").RawValue as string;
                return(true);
            }

            // If we have reached this point then we haven't been able to bind id or ref type and there is no content
            // We might still have a get all function with no id which we need to cater for e.g. ReadAllGenders. In this
            // instance set the result to has key with a null key and let the back end deal with it
            result.HasKey = true;
            return(true);

            //bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Cannot convert value to Location");
            //return false;
        }
Esempio n. 2
0
        public virtual async Task <IHttpActionResult> Search(ODataQueryOptions <E> request)
        {
            try
            {
                //Validate that the request is correctly formed.
                request.Validate(mODataValidate);

                //Load the request with the necessary parameters.
                SearchRequest rq = new SearchRequest();
                rq.ODataPopulate(request);

                //Make the request to the persistence service.
                RepositorySettings settings = ApiUtility.BuildRepositorySettings(ActionContext);
                RepositoryHolder <SearchRequest, SearchResponse> response = await mRespository.Search(rq, settings);

                return(new OData4ServiceDocumentResponse(response, ActionContext.Request.RequestUri));
            }
            catch (Exception vex)
            {
                return(BadRequest($"Unable to process Odata request - {vex.Message}"));
            }
        }