Example #1
0
        public override Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider,
                                                 HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            var headerValue = actionContext.Request.Headers.GetHeaderValue(Descriptor.ParameterName);
            var type        = GetNonNullableType(Descriptor.ParameterType);

            var isNullOrEmpty = string.IsNullOrEmpty(headerValue);

            if (type == typeof(string) && isNullOrEmpty)
            {
                headerValue = "";
            }

            if (type != typeof(string) && type == Descriptor.ParameterType && string.IsNullOrEmpty(headerValue))
            {
                actionContext.ModelState.AddModelError(Descriptor.ParameterName, string.Format(HEADER_MISSING, Descriptor.ParameterName));
            }
            else
            {
                var converted = ConvertTo(Descriptor.ParameterType, headerValue);
                if (converted == null)
                {
                    actionContext.ModelState.AddModelError(Descriptor.ParameterName, string.Format(HEADER_INCORRECT_FORMAT, Descriptor.ParameterName));
                }
                actionContext.ActionArguments[Descriptor.ParameterName] = converted;
            }

            return(Task.FromResult(default(AsyncVoid)));
        }
Example #2
0
        public override System.Threading.Tasks.Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            if (actionContext.Request.Method.Equals(System.Net.Http.HttpMethod.Get))
            {
                var request = System.Web.HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
                SetValue(actionContext, new NameValueCollection(request));
            }
            else
            if (actionContext.Request.Method.Equals(System.Net.Http.HttpMethod.Post))
            {
                object outObj;
                var    jsonText = "";
                if (actionContext.Request.Properties.TryGetValue("MS_HttpContext", out outObj))
                {
                    var context = outObj as HttpContextWrapper;
                    context.Request.InputStream.Position = 0;
                    using (StreamReader sr = new StreamReader(context.Request.InputStream)) {
                        jsonText = sr.ReadToEnd();
                    }
                }
                var jobj = JObject.Parse(jsonText);
                SetValue(actionContext, jobj);
            }

            TaskCompletionSource <AsyncVoid> tcs = new TaskCompletionSource <AsyncVoid>();

            tcs.SetResult(default(AsyncVoid));
            return(tcs.Task);
        }
    public override Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
    {
        var value = actionContext.ControllerContext.RouteData.Values[Descriptor.ParameterName].ToString();

        SetValue(actionContext, ObjectId.Parse(value));
        var tsc = new TaskCompletionSource <object>();

        tsc.SetResult(null);
        return(tsc.Task);
    }
Example #4
0
    public override System.Threading.Tasks.Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
    {
        var id = int.Parse(actionContext.Request.RequestUri.Segments.Last());

        SetValue(actionContext, string.Format("This is formatted ID value:{0}", id));
        var tsc = new TaskCompletionSource <object>();

        tsc.SetResult(null);
        return(tsc.Task);
    }
Example #5
0
 public bool Validate(object model, Type type, System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, System.Web.Http.Controllers.HttpActionContext actionContext, string keyPrefix)
 {
     if (model.GetType().GetCustomAttributes(true).Contains(new SkipModelValidationAttribute()))
     {
         return(true);
     }
     else
     {
         return(_defaultValidator.Validate(model, type, metadataProvider, actionContext, keyPrefix));
     }
 }
        public override System.Threading.Tasks.Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            var request = System.Web.HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);

            SetValue(actionContext, new NameValueCollection(request));

            TaskCompletionSource <AsyncVoid> tcs = new TaskCompletionSource <AsyncVoid>();

            tcs.SetResult(default(AsyncVoid));
            return(tcs.Task);
        }
Example #7
0
        public override Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            object value      = null;
            var    webContext = WebHelper.GetWebCallContext(actionContext.Request);

            if (webContext != null)
            {
                value = webContext.OperationContext;
            }
            actionContext.ActionArguments[base.Descriptor.ParameterName] = value;
            return(_defaultCompleted);
        }
        public override System.Threading.Tasks.Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            var request     = System.Web.HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
            var proxy       = new Proxy(Commons.UserId, Commons.ServerDateTime, Commons.DateFormat, Commons.Culture);
            var proxyHelper = new DataProxyHelper <NameValueCollection>(proxy, new NameValueCollection(request));

            SetValue(actionContext, proxyHelper);

            TaskCompletionSource <AsyncVoid> tcs = new TaskCompletionSource <AsyncVoid>();

            tcs.SetResult(default(AsyncVoid));
            return(tcs.Task);
        }
Example #9
0
        public override Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            var routeValues = actionContext.ControllerContext.RouteData.Values;

            if (routeValues[_parameterName] != null)
            {
                string[] vals = routeValues[_parameterName].ToString().Split(_delimiter);

                actionContext.ActionArguments.Add(_parameterName, vals.Where(v => !v.Trim().IsNullOrEmpty()).ToArray());
            }
            else
            {
                actionContext.ActionArguments.Add(_parameterName, new string[0]);
            }

            return(Task.FromResult(0));
        }
        public override Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider,
                                                 HttpActionContext actionContext,
                                                 CancellationToken cancellationToken)
        {
            var binding = actionContext
                          .ActionDescriptor
                          .ActionBinding;

            if (binding.ParameterBindings.Length > 1 ||
                actionContext.Request.Method == HttpMethod.Get)
            {
                return(EmptyTask.Start());
            }

            var type = binding
                       .ParameterBindings[0]
                       .Descriptor.ParameterType;

            if (type == typeof(string))
            {
                return(actionContext.Request.Content
                       .ReadAsStringAsync()
                       .ContinueWith((task) =>
                {
                    var stringResult = task.Result;
                    SetValue(actionContext, stringResult);
                }));
            }
            else if (type == typeof(byte[]))
            {
                return(actionContext.Request.Content
                       .ReadAsByteArrayAsync()
                       .ContinueWith((task) =>
                {
                    byte[] result = task.Result;
                    SetValue(actionContext, result);
                }));
            }

            throw new InvalidOperationException("Only string and byte[] are supported for [NakedBody] parameters");
        }
        public override Task ExecuteBindingAsync(
            System.Web.Http.Metadata.ModelMetadataProvider metadataProvider,
            HttpActionContext actionContext,
            CancellationToken cancellationToken)
        {
            var routeValues = actionContext.ControllerContext.RouteData.Values;

            if (routeValues[_parameterName] != null)
            {
                string[] catchAllValues =
                    routeValues[_parameterName].ToString().Split(_delimiter);

                actionContext.ActionArguments.Add(_parameterName, catchAllValues);
            }
            else
            {
                actionContext.ActionArguments.Add(_parameterName, new string[0]);
            }

            return(TaskHelpers.Completed());
        }
        public override Task ExecuteBindingAsync(System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, HttpActionContext actionContext, System.Threading.CancellationToken cancellationToken)
        {
            var binding = actionContext
                          .ActionDescriptor
                          .ActionBinding;

            if (binding.ParameterBindings.Where(p => p.WillReadBody).Count() > 1 || actionContext.Request.Method == HttpMethod.Get)
            {
                throw new InvalidOperationException("Only a single parameter that will read from the body of the message can be bound to this type of binding, and it cannot be used for a Get.");
            }

            //var type = binding
            //            .ParameterBindings[0]
            //            .Descriptor.ParameterType;
            var type = binding.ParameterBindings.Where(p => p.WillReadBody).First().Descriptor.ParameterType;

            if (type == typeof(string))
            {
                return(actionContext.Request.Content
                       .ReadAsStringAsync()
                       .ContinueWith((task) =>
                {
                    var stringResult = task.Result;
                    SetValue(actionContext, stringResult);
                }));
            }
            else if (type == typeof(byte[]) || type == typeof(IEnumerable <byte>))
            {
                return(actionContext.Request.Content
                       .ReadAsByteArrayAsync()
                       .ContinueWith((task) =>
                {
                    byte[] result = task.Result;
                    SetValue(actionContext, result);
                }));
            }

            throw new InvalidOperationException("Only string and byte[] are supported for [RawBody] parameters");
        }
Example #13
0
 public bool Validate(object model, Type type, System.Web.Http.Metadata.ModelMetadataProvider metadataProvider, System.Web.Http.Controllers.HttpActionContext actionContext, string keyPrefix)
 {
     return(true);
 }