コード例 #1
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            // Specify a default argument name if none is set by ModelBinderAttribute
            //var modelName = bindingContext.BinderModelName;
            //if (string.IsNullOrEmpty(modelName))
            //{
            //    modelName = "statements";
            //}

            var request = bindingContext.ActionContext.HttpContext.Request;

            try
            {
                var       jsonModelReader = new JsonModelReader(request.Headers, request.Body);
                Statement statement       = await jsonModelReader.ReadAs <Statement>();

                if (statement != null)
                {
                    bindingContext.Result = ModelBindingResult.Success(statement);
                }
                else
                {
                    bindingContext.Result = ModelBindingResult.Failed();
                }
            }
            catch (JsonModelReaderException ex)
            {
                throw new BadRequestException(ex.Message);
            }
        }
コード例 #2
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(Statement))
            {
                return(false);
            }
            var modelName = bindingContext.ModelName;
            var body      = actionContext.Request.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
            var headers   = actionContext.Request.Content.Headers;

            try
            {
                var       jsonModelReader = new JsonModelReader(headers, body);
                Statement statement       = jsonModelReader.ReadAs <Statement>().GetAwaiter().GetResult();
                if (statement != null)
                {
                    var validator = new StatementValidator();
                    var results   = validator.Validate(statement);
                    if (!results.IsValid)
                    {
                        results.AddToModelState(bindingContext.ModelState, null);
                        return(false);
                    }
                    bindingContext.Model = statement;
                    return(true);
                }
            } catch (JsonModelReaderException ex)
            {
                bindingContext.ModelState.AddModelError(modelName, ex);
                return(false);
            }

            return(false);
        }
コード例 #3
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(PostStatementContent))
            {
                return;
            }

            var model = new PostStatementContent();

            var request = bindingContext.ActionContext.HttpContext.Request;

            //string strContentType = request.ContentType ?? MediaTypes.Application.Json;

            //try
            //{
            //    var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(strContentType);
            //}
            //catch (FormatException ex)
            //{
            //    throw new BadRequestException(ex.InnerException?.Message ?? ex.Message, ex);
            //}

            try
            {
                var jsonModelReader = new JsonModelReader(request.Headers, request.Body);
                model.Statements = await jsonModelReader.ReadAs <StatementCollection>();
            }
            catch (JsonModelReaderException ex)
            {
                throw new BadRequestException(ex.InnerException?.Message ?? ex.Message, ex);
            }

            if (model.Statements == null)
            {
                bindingContext.Result = ModelBindingResult.Failed();
            }
            else
            {
                bindingContext.Result = ModelBindingResult.Success(model);
            }
        }
コード例 #4
0
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(StatementCollection))
            {
                return;
            }

            try
            {
                var request         = bindingContext.ActionContext.HttpContext.Request;
                var jsonModelReader = new JsonModelReader(request.Headers, request.Body);

                var statements = await jsonModelReader.ReadAs <StatementCollection>();

                bindingContext.Result = ModelBindingResult.Success(statements);
                return;
            }
            catch (JsonModelReaderException ex)
            {
                bindingContext.ModelState.TryAddModelException <StatementCollection>(x => x, ex);
                bindingContext.Result = ModelBindingResult.Failed();
            }
        }