示例#1
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw Error.ArgumentNull("bindingContext");
            }

            if (bindingContext.ModelMetadata == null)
            {
                throw Error.Argument("bindingContext", SRResources.ModelBinderUtil_ModelMetadataCannotBeNull);
            }

            ValueProviderResult valueProviderResult = ValueProviderResult.None;
            string modelName = ODataParameterValue.ParameterValuePrefix + bindingContext.ModelName;

            try
            {
                // Look in route data for a ODataParameterValue.
                object valueAsObject = null;
                if (!bindingContext.HttpContext.Request.ODataFeature().RoutingConventionsStore.TryGetValue(modelName, out valueAsObject))
                {
                    bindingContext.ActionContext.RouteData.Values.TryGetValue(modelName, out valueAsObject);
                }

                if (valueAsObject != null)
                {
                    StringValues stringValues = new StringValues(valueAsObject.ToString());
                    valueProviderResult = new ValueProviderResult(stringValues);
                    bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

                    ODataParameterValue paramValue = valueAsObject as ODataParameterValue;
                    if (paramValue != null)
                    {
                        HttpRequest request = bindingContext.HttpContext.Request;
                        object      model   = ConvertTo(paramValue, bindingContext, request.GetRequestContainer());
                        bindingContext.Result = ModelBindingResult.Success(model);
                        return(Task.CompletedTask);
                    }
                }
                else
                {
                    // If not in the route data, ask the value provider.
                    valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
                    if (valueProviderResult == ValueProviderResult.None)
                    {
                        valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                    }

                    if (valueProviderResult != ValueProviderResult.None)
                    {
                        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

                        object model = ODataModelBinderConverter.ConvertTo(valueProviderResult.FirstValue, bindingContext.ModelType);
                        if (model != null)
                        {
                            bindingContext.Result = ModelBindingResult.Success(model);
                            return(Task.CompletedTask);
                        }
                    }
                }

                // No matches, binding failed.
                bindingContext.Result = ModelBindingResult.Failed();
            }
            catch (ODataException ex)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
                bindingContext.Result = ModelBindingResult.Failed();
            }
            catch (ValidationException ex)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, Error.Format(SRResources.ValueIsInvalid, valueProviderResult.FirstValue, ex.Message));
                bindingContext.Result = ModelBindingResult.Failed();
            }
            catch (FormatException ex)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, Error.Format(SRResources.ValueIsInvalid, valueProviderResult.FirstValue, ex.Message));
                bindingContext.Result = ModelBindingResult.Failed();
            }
            catch (Exception e)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, e.Message);
                bindingContext.Result = ModelBindingResult.Failed();
            }

            return(Task.CompletedTask);
        }
            public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
            {
                if (bindingContext == null)
                {
                    throw Error.ArgumentNull("bindingContext");
                }

                if (bindingContext.ModelMetadata == null)
                {
                    throw Error.Argument("bindingContext", SRResources.ModelBinderUtil_ModelMetadataCannotBeNull);
                }

                string modelName          = ODataParameterValue.ParameterValuePrefix + bindingContext.ModelName;
                ValueProviderResult value = bindingContext.ValueProvider.GetValue(modelName);

                if (value == null)
                {
                    value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
                    if (value == null)
                    {
                        return(false);
                    }
                }

                bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);

                try
                {
                    ODataParameterValue paramValue = value.RawValue as ODataParameterValue;
                    if (paramValue != null)
                    {
                        bindingContext.Model = ConvertTo(paramValue, actionContext, bindingContext,
                                                         actionContext.Request.GetRequestContainer());
                        return(true);
                    }

                    string valueString = value.RawValue as string;
                    if (valueString != null)
                    {
                        bindingContext.Model = ODataModelBinderConverter.ConvertTo(valueString, bindingContext.ModelType);
                        return(true);
                    }

                    return(false);
                }
                catch (ODataException ex)
                {
                    bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
                    return(false);
                }
                catch (ValidationException ex)
                {
                    bindingContext.ModelState.AddModelError(bindingContext.ModelName, Error.Format(SRResources.ValueIsInvalid, value.RawValue, ex.Message));
                    return(false);
                }
                catch (FormatException ex)
                {
                    bindingContext.ModelState.AddModelError(bindingContext.ModelName, Error.Format(SRResources.ValueIsInvalid, value.RawValue, ex.Message));
                    return(false);
                }
                catch (Exception e)
                {
                    bindingContext.ModelState.AddModelError(bindingContext.ModelName, e);
                    return(false);
                }
            }