Пример #1
0
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            const string modelName           = "filterBy";
            var          valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);

            if (valueProviderResult == ValueProviderResult.None)
            {
                return(Task.CompletedTask);
            }

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

            var filterExpressions = valueProviderResult.Values.ToString().Split(",");
            var parsedFilter      = FilterOptions.FromArray(filterExpressions);

            if (parsedFilter.GetKeyValuePairs().Count() != valueProviderResult.Length)
            {
                const string errorMessage = "Failed to convert all filter expressions in URI. Maybe a colon is missing.";
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, errorMessage);

                if (parsedFilter.HasPredicates == false)
                {
                    bindingContext.Result = ModelBindingResult.Failed();
                    return(Task.CompletedTask);
                }
                // Else: Result not ModelBindingResult.Failed() because some expressions could be parsed.
            }

            bindingContext.Model  = parsedFilter;
            bindingContext.Result = ModelBindingResult.Success(parsedFilter);

            return(Task.CompletedTask);
        }