public override Task ExecuteBindingAsync( ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) { var paramFromBody = Descriptor; var type = paramFromBody.ParameterType; var request = actionContext.ControllerContext.Request; var formatterLogger = new ModelStateFormatterLogger(actionContext.ModelState, paramFromBody.ParameterName); return ExecuteBindingAsyncCore(metadataProvider, actionContext, paramFromBody, type, request, formatterLogger, cancellationToken); }
public void LogErrorAddsErrorToModelState() { ModelStateDictionary modelState = new ModelStateDictionary(); string prefix = "prefix"; IFormatterLogger formatterLogger = new ModelStateFormatterLogger(modelState, prefix); formatterLogger.LogError("property", "error"); Assert.True(modelState.ContainsKey("prefix.property")); Assert.Equal(1, modelState["prefix.property"].Errors.Count); Assert.Equal("error", modelState["prefix.property"].Errors[0].ErrorMessage); }
public async Task<ArgumentBinding[]> BindParameters(IInvokeable nm, HttpRequestMessage request, CancellationToken cancellationToken) { var config = (HttpConfiguration) request.Properties["MS_HttpConfiguration"]; var formatter = config.Formatters.FindReader(nm.ParameterType, request.Content.Headers.ContentType); //formatter = new Noodles.WebApi.JQueryMvcFormUrlEncodedFormatter(); var stream = await request.Content.ReadAsStreamAsync(); var modelState = new ModelStateDictionary(); var logger = new ModelStateFormatterLogger(modelState, "model"); var obj = await formatter.ReadFromStreamAsync(nm.ParameterType, stream, request.Content, logger); if (obj == null) return Enumerable.Empty<ArgumentBinding>().ToArray(); throw new NotImplementedException("Aargh"); //return obj.GetType().GetProperties().Select(pi => // new ArgumentBinding(){ Parameter = pi, pi.Name, pi.GetValue(obj, null))).ToArray(); }
public void LogErrorAddsExceptionToModelState() { ModelStateDictionary modelState = new ModelStateDictionary(); string prefix = "prefix"; IFormatterLogger formatterLogger = new ModelStateFormatterLogger(modelState, prefix); Exception e = new Exception("error"); formatterLogger.LogError("property", e); Assert.True(modelState.ContainsKey("prefix.property")); ModelError error = Assert.Single(modelState["prefix.property"].Errors); Assert.Equal(e, error.Exception); }
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken) { HttpParameterDescriptor paramFromBody = this.Descriptor; Type type = paramFromBody.ParameterType; HttpRequestMessage request = actionContext.ControllerContext.Request; IFormatterLogger formatterLogger = new ModelStateFormatterLogger(actionContext.ModelState, paramFromBody.ParameterName); Task<object> task = ReadContentAsync(request, type, _formatters, formatterLogger); return task.Then( (model) => { // Put the parameter result into the action context. string name = paramFromBody.ParameterName; actionContext.ActionArguments.Add(name, model); // validate the object graph. // null indicates we want no body parameter validation if (BodyModelValidator != null) { BodyModelValidator.Validate(model, type, metadataProvider, actionContext, paramFromBody.ParameterName); } }); }