예제 #1
0
 public Task <BindResult> Bind(RequestBinderContext context)
 {
     GetParameters().ForEach(x =>
     {
         if (x.GetArgument(context.ActionArguments) == null)
         {
             x.BindArgument(context.ActionArguments, _container
                            .GetInstance(x.TypeDescriptor.Type));
         }
     });
     return(BindResult.Success().ToTaskResult());
 }
예제 #2
0
        public virtual BindResult Bind(ILookup <string, object> values, object[] actionArguments,
                                       IEnumerable <ActionParameter> parameters, Func <ActionParameter, string> mapName = null)
        {
            if (values == null || !values.Any())
            {
                return(BindResult.Success());
            }

            var actionParameters = parameters.Where(x => x.IsParameter || x.IsPropertyOfParameter);

            return(_parameterBinder.Bind(values, actionParameters,
                                         (p, v) => p.BindArgument(actionArguments, v),
                                         BindResult.Success, BindResult.Failure, mapName));
        }
예제 #3
0
        private BindResult BindValues(List <KeyValuePair <string, object> > values,
                                      RequestBinderContext context)
        {
            if (!values.Any())
            {
                return(BindResult.Success());
            }

            var valueLookup = values.ToLookup();

            if (_actionDescriptor.Route.Parameters.Any())
            {
                var result = _argumentBinder.Bind(valueLookup,
                                                  context.ActionArguments, _actionDescriptor.Route.Parameters);
                if (result.Status == BindingStatus.Failure)
                {
                    return(result);
                }
            }

            if (!_actionDescriptor.Route.HasRequest)
            {
                return(BindResult.Success());
            }

            var requestParameter = _actionDescriptor.Route.RequestParameter;

            if (IsEnumerableStream(requestParameter.ParameterType))
            {
                return(BindResult.Success());
            }

            var instance = EnsureRequestValue(context, requestParameter);

            var actionParameters = requestParameter.ParameterType.Properties
                                   .Select(x => new ActionParameter(_actionDescriptor.Action, requestParameter, x));

            return(_parameterBinder.Bind(valueLookup, actionParameters,
                                         (p, v) => p.BindProperty(instance, v),
                                         BindResult.Success, BindResult.Failure));
        }
예제 #4
0
        private async Task <BindResult> Read(HttpContent content,
                                             ParameterDescriptor requestParameter,
                                             RequestBinderContext context)
        {
            var readContext = content.CreateReaderContext(_actionDescriptor);
            var reader      = _actionDescriptor.RequestReaders
                              .ThatApplyToOrDefault(_readers, readContext)
                              .FirstOrDefault();

            if (reader == null)
            {
                return(BindResult.NoReader());
            }

            var result = await reader.Read(readContext);

            if (result.Status == ReadStatus.Failure)
            {
                return(BindResult.Failure(result.ErrorMessage));
            }
            context.ActionArguments[requestParameter.Position] = result.Value;
            return(BindResult.Success());
        }
예제 #5
0
        public virtual async Task <BindResult> Bind(RequestBinderContext context)
        {
            var position = _actionDescriptor.Route.RequestParameter.Position;
            var headers  = _requestMessage.Content?.Headers;

            if ((headers?.ContentLength ?? 0) == 0)
            {
                if (_configuration.CreateEmptyRequestParameterValue)
                {
                    context.ActionArguments[position] = _actionDescriptor
                                                        .Route.RequestParameter.ParameterType.TryCreate();
                }
                return(BindResult.Success());
            }

            var readContext = _requestMessage.Content
                              .CreateReaderContext(_actionDescriptor);
            var reader = _actionDescriptor.RequestReaders
                         .ThatApplyToOrDefault(_readers, readContext)
                         .FirstOrDefault();

            if (reader == null)
            {
                return(BindResult.NoReader());
            }

            var result = await reader.Read(readContext);

            if (result.Status == ReadStatus.Failure)
            {
                return(BindResult.Failure(result.ErrorMessage));
            }

            context.ActionArguments[position] = result.Value;
            return(BindResult.Success());
        }
예제 #6
0
        public async Task <BindResult> Bind(RequestBinderContext context)
        {
            var route            = _actionDescriptor.Route;
            var requestParameter = route.RequestParameter;
            var stream           = await _requestMessage.Content.ReadAsStreamAsync();

            var multipartContent = new MultipartContent(stream,
                                                        _requestMessage.Content.Headers, _configuration);
            var values = new List <KeyValuePair <string, object> >();

            while (true)
            {
                var content = multipartContent.Peek();

                if (content == null)
                {
                    break;
                }

                if (content.Error)
                {
                    return(new BindResult(BindingStatus.Failure, content.ErrorMessage));
                }

                if (route.HasRequest)
                {
                    var result = await Read(content, requestParameter, context);

                    if (result.Status == BindingStatus.Failure)
                    {
                        return(result);
                    }
                    if (result.Status == BindingStatus.Success)
                    {
                        multipartContent.Pop();
                        continue;
                    }
                }

                if (route.HasParameterNamed(content.Name) ||
                    requestParameter.HasPropertyNamed(content.Name))
                {
                    values.Add(content.Name, await content.ReadAsStringAsync());
                    multipartContent.Pop();
                    continue;
                }

                break;
            }

            var bindResult = BindValues(values, context);

            if (bindResult.Status == BindingStatus.Failure)
            {
                return(bindResult);
            }

            BindMultipartParameters(context, multipartContent);
            BindMultipartProperties(requestParameter, context, multipartContent);

            return(BindResult.Success());
        }