From d8cc2b85d55af47c4038cb8ae44ab46a47af3b03 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 25 Nov 2015 14:05:38 -0800 Subject: [PATCH] Adds ActionContext to Validation contexts This change makes it possible to access the ActionContext/ActionDescriptor from inside of validators and client validators. --- .../ModelBinding/ModelBindingContext.cs | 8 +- .../ClientModelValidationContext.cs | 20 +- .../Validation/ModelValidationContext.cs | 5 + .../DefaultControllerActionArgumentBinder.cs | 14 +- .../ModelBinding/ModelBindingHelper.cs | 90 +++------ .../Validation/DefaultObjectValidator.cs | 12 +- .../Validation/IObjectModelValidator.cs | 6 +- .../Validation/ValidationVisitor.cs | 19 +- .../Controller.cs | 11 +- .../RemoteAttribute.cs | 3 +- .../ViewFeatures/DefaultHtmlGenerator.cs | 4 +- .../ApiController.cs | 2 +- .../ControllerActionArgumentBinderTests.cs | 22 +-- .../ModelBinding/ModelBindingHelperTest.cs | 39 ++-- .../Validation/DefaultObjectValidatorTests.cs | 120 +++++++----- .../CompareAttributeAdapterTest.cs | 27 +-- .../MaxLengthAttributeAdapterTest.cs | 23 +-- .../MinLengthAttributeAdapterTest.cs | 15 +- .../NumericClientModelValidatorTest.cs | 9 +- .../RangeAttributeAdapterTest.cs | 8 +- .../RequiredAttributeAdapterTest.cs | 8 +- .../StringLengthAttributeAdapterTest.cs | 15 +- .../ActionParametersIntegrationTest.cs | 32 ++-- .../ArrayModelBinderIntegrationTest.cs | 32 ++-- ...nderTypeBasedModelBinderIntegrationTest.cs | 28 +-- .../BodyValidationIntegrationTests.cs | 28 +-- .../ByteArrayModelBinderIntegrationTest.cs | 12 +- ...ellationTokenModelBinderIntegrationTest.cs | 12 +- .../CollectionModelBinderIntegrationTest.cs | 68 +++---- .../DictionaryModelBinderIntegrationTest.cs | 44 ++--- ...rmCollectionModelBindingIntegrationTest.cs | 12 +- .../FormFileModelBindingIntegrationTest.cs | 12 +- .../GenericModelBinderIntegrationTest.cs | 56 +++--- .../HeaderModelBinderIntegrationTest.cs | 16 +- .../KeyValuePairModelBinderIntegrationTest.cs | 48 ++--- .../ModelPrefixSelectionIntegrationTest.cs | 20 +- ...MutableObjectModelBinderIntegrationTest.cs | 172 +++++++++--------- .../ServicesModelBinderIntegrationTest.cs | 19 +- .../SimpleTypeModelBinderIntegrationTest.cs | 40 ++-- .../TryUpdateModelIntegrationTest.cs | 92 +++++----- .../ValidationIntegrationTests.cs | 100 +++++----- .../RemoteAttributeTest.cs | 24 ++- 42 files changed, 658 insertions(+), 689 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs index e32b97f063..a89d58fe6c 100644 --- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs +++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/ModelBindingContext.cs @@ -37,7 +37,6 @@ public ModelBindingContext() /// A new instance of . public static ModelBindingContext CreateBindingContext( OperationBindingContext operationBindingContext, - ModelStateDictionary modelState, ModelMetadata metadata, BindingInfo bindingInfo, string modelName) @@ -47,11 +46,6 @@ public static ModelBindingContext CreateBindingContext( throw new ArgumentNullException(nameof(operationBindingContext)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (metadata == null) { throw new ArgumentNullException(nameof(metadata)); @@ -83,7 +77,7 @@ public static ModelBindingContext CreateBindingContext( IsTopLevelObject = true, ModelMetadata = metadata, - ModelState = modelState, + ModelState = operationBindingContext.ActionContext.ModelState, OperationBindingContext = operationBindingContext, ValueProvider = operationBindingContext.ValueProvider, diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs index e1a8d30e00..037f28bbe7 100644 --- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs +++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ClientModelValidationContext.cs @@ -8,10 +8,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation public class ClientModelValidationContext { public ClientModelValidationContext( + ActionContext actionContext, ModelMetadata metadata, - IModelMetadataProvider metadataProvider, - IServiceProvider requestServices) + IModelMetadataProvider metadataProvider) { + if (actionContext == null) + { + throw new ArgumentNullException(nameof(actionContext)); + } + if (metadata == null) { throw new ArgumentNullException(nameof(metadata)); @@ -22,20 +27,15 @@ public ClientModelValidationContext( throw new ArgumentNullException(nameof(metadataProvider)); } - if (requestServices == null) - { - throw new ArgumentNullException(nameof(requestServices)); - } - + ActionContext = actionContext; ModelMetadata = metadata; MetadataProvider = metadataProvider; - RequestServices = requestServices; } + public ActionContext ActionContext { get; } + public ModelMetadata ModelMetadata { get; } public IModelMetadataProvider MetadataProvider { get; } - - public IServiceProvider RequestServices { get; } } } diff --git a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs index d600ce7e94..cf8c72b249 100644 --- a/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs +++ b/src/Microsoft.AspNet.Mvc.Abstractions/ModelBinding/Validation/ModelValidationContext.cs @@ -8,6 +8,11 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Validation /// public class ModelValidationContext { + /// + /// Gets or sets the + /// + public ActionContext ActionContext { get; set; } + /// /// Gets or sets the model object. /// diff --git a/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs b/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs index 2d90a3caa9..34d8d2acdd 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controllers/DefaultControllerActionArgumentBinder.cs @@ -82,7 +82,6 @@ private async Task> BindActionArgumentsCoreAsync( var controllerProperties = new Dictionary(StringComparer.Ordinal); await PopulateArgumentsAsync( operationBindingContext, - context.ModelState, controllerProperties, actionDescriptor.BoundProperties); var controllerType = actionDescriptor.ControllerTypeInfo.AsType(); @@ -91,7 +90,6 @@ await PopulateArgumentsAsync( var actionArguments = new Dictionary(StringComparer.Ordinal); await PopulateArgumentsAsync( operationBindingContext, - context.ModelState, actionArguments, actionDescriptor.Parameters); return actionArguments; @@ -99,7 +97,6 @@ await PopulateArgumentsAsync( public async Task BindModelAsync( ParameterDescriptor parameter, - ModelStateDictionary modelState, OperationBindingContext operationContext) { if (parameter == null) @@ -107,11 +104,6 @@ public async Task BindModelAsync( throw new ArgumentNullException(nameof(parameter)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (operationContext == null) { throw new ArgumentNullException(nameof(operationContext)); @@ -120,7 +112,6 @@ public async Task BindModelAsync( var metadata = _modelMetadataProvider.GetMetadataForType(parameter.ParameterType); var modelBindingContext = ModelBindingContext.CreateBindingContext( operationContext, - modelState, metadata, parameter.BindingInfo, parameter.Name); @@ -129,8 +120,8 @@ public async Task BindModelAsync( if (modelBindingResult.IsModelSet) { _validator.Validate( + operationContext.ActionContext, operationContext.ValidatorProvider, - modelState, modelBindingContext.ValidationState, modelBindingResult.Key, modelBindingResult.Model); @@ -204,7 +195,6 @@ private void ActivateProperties(object controller, Type containerType, Dictionar private async Task PopulateArgumentsAsync( OperationBindingContext operationContext, - ModelStateDictionary modelState, IDictionary arguments, IList parameterMetadata) { @@ -212,7 +202,7 @@ private async Task PopulateArgumentsAsync( for (var i = 0; i < parameterMetadata.Count; i++) { var parameter = parameterMetadata[i]; - var modelBindingResult = await BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await BindModelAsync(parameter, operationContext); if (modelBindingResult.IsModelSet) { arguments[parameter.Name] = modelBindingResult.Model; diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs index b42b31c55e..8764339924 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/ModelBindingHelper.cs @@ -29,8 +29,6 @@ public static class ModelBindingHelper /// The prefix to use when looking up values in the . /// /// The for the current executing request. - /// The used for maintaining state and - /// results of model-binding validation. /// The provider used for reading metadata for the model type. /// The used for binding. /// The used for looking up values. @@ -46,7 +44,6 @@ public static Task TryUpdateModelAsync( TModel model, string prefix, ActionContext actionContext, - ModelStateDictionary modelState, IModelMetadataProvider metadataProvider, IModelBinder modelBinder, IValueProvider valueProvider, @@ -70,11 +67,6 @@ public static Task TryUpdateModelAsync( throw new ArgumentNullException(nameof(actionContext)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); @@ -110,7 +102,6 @@ public static Task TryUpdateModelAsync( model, prefix, actionContext, - modelState, metadataProvider, modelBinder, valueProvider, @@ -130,8 +121,6 @@ public static Task TryUpdateModelAsync( /// The prefix to use when looking up values in the . /// /// The for the current executing request. - /// The used for maintaining state and - /// results of model-binding validation. /// The provider used for reading metadata for the model type. /// The used for binding. /// The used for looking up values. @@ -147,17 +136,16 @@ public static Task TryUpdateModelAsync( /// which need to be included for the current model. /// A that on completion returns true if the update is successful public static Task TryUpdateModelAsync( - TModel model, - string prefix, - ActionContext actionContext, - ModelStateDictionary modelState, - IModelMetadataProvider metadataProvider, - IModelBinder modelBinder, - IValueProvider valueProvider, - IList inputFormatters, - IObjectModelValidator objectModelValidator, - IModelValidatorProvider validatorProvider, - params Expression>[] includeExpressions) + TModel model, + string prefix, + ActionContext actionContext, + IModelMetadataProvider metadataProvider, + IModelBinder modelBinder, + IValueProvider valueProvider, + IList inputFormatters, + IObjectModelValidator objectModelValidator, + IModelValidatorProvider validatorProvider, + params Expression>[] includeExpressions) where TModel : class { if (model == null) @@ -175,11 +163,6 @@ public static Task TryUpdateModelAsync( throw new ArgumentNullException(nameof(actionContext)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); @@ -222,7 +205,6 @@ public static Task TryUpdateModelAsync( model, prefix, actionContext, - modelState, metadataProvider, modelBinder, valueProvider, @@ -242,8 +224,6 @@ public static Task TryUpdateModelAsync( /// The prefix to use when looking up values in the . /// /// The for the current executing request. - /// The used for maintaining state and - /// results of model-binding validation. /// The provider used for reading metadata for the model type. /// The used for binding. /// The used for looking up values. @@ -258,18 +238,17 @@ public static Task TryUpdateModelAsync( /// filter properties(for inclusion/exclusion) at runtime. /// A that on completion returns true if the update is successful public static Task TryUpdateModelAsync( - TModel model, - string prefix, - ActionContext actionContext, - ModelStateDictionary modelState, - IModelMetadataProvider metadataProvider, - IModelBinder modelBinder, - IValueProvider valueProvider, - IList inputFormatters, - IObjectModelValidator objectModelValidator, - IModelValidatorProvider validatorProvider, - Func predicate) - where TModel : class + TModel model, + string prefix, + ActionContext actionContext, + IModelMetadataProvider metadataProvider, + IModelBinder modelBinder, + IValueProvider valueProvider, + IList inputFormatters, + IObjectModelValidator objectModelValidator, + IModelValidatorProvider validatorProvider, + Func predicate) + where TModel : class { if (model == null) { @@ -286,11 +265,6 @@ public static Task TryUpdateModelAsync( throw new ArgumentNullException(nameof(actionContext)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); @@ -331,7 +305,6 @@ public static Task TryUpdateModelAsync( typeof(TModel), prefix, actionContext, - modelState, metadataProvider, modelBinder, valueProvider, @@ -351,8 +324,6 @@ public static Task TryUpdateModelAsync( /// The prefix to use when looking up values in the . /// /// The for the current executing request. - /// The used for maintaining state and - /// results of model-binding validation. /// The provider used for reading metadata for the model type. /// The used for binding. /// The used for looking up values. @@ -369,7 +340,6 @@ public static Task TryUpdateModelAsync( Type modelType, string prefix, ActionContext actionContext, - ModelStateDictionary modelState, IModelMetadataProvider metadataProvider, IModelBinder modelBinder, IValueProvider valueProvider, @@ -397,11 +367,6 @@ public static Task TryUpdateModelAsync( throw new ArgumentNullException(nameof(actionContext)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); @@ -438,7 +403,6 @@ public static Task TryUpdateModelAsync( modelType, prefix, actionContext, - modelState, metadataProvider, modelBinder, valueProvider, @@ -458,8 +422,6 @@ public static Task TryUpdateModelAsync( /// The prefix to use when looking up values in the . /// /// The for the current executing request. - /// The used for maintaining state and - /// results of model-binding validation. /// The provider used for reading metadata for the model type. /// The used for binding. /// The used for looking up values. @@ -478,7 +440,6 @@ public static async Task TryUpdateModelAsync( Type modelType, string prefix, ActionContext actionContext, - ModelStateDictionary modelState, IModelMetadataProvider metadataProvider, IModelBinder modelBinder, IValueProvider valueProvider, @@ -507,11 +468,6 @@ public static async Task TryUpdateModelAsync( throw new ArgumentNullException(nameof(actionContext)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); @@ -558,6 +514,7 @@ public static async Task TryUpdateModelAsync( var modelMetadata = metadataProvider.GetMetadataForType(modelType); // Clear ModelStateDictionary entries for the model so that it will be re-validated. + var modelState = actionContext.ModelState; ClearValidationStateForModel(modelType, modelState, metadataProvider, prefix); var operationBindingContext = new OperationBindingContext @@ -572,7 +529,6 @@ public static async Task TryUpdateModelAsync( var modelBindingContext = ModelBindingContext.CreateBindingContext( operationBindingContext, - modelState, modelMetadata, bindingInfo: null, modelName: prefix ?? string.Empty); @@ -583,8 +539,8 @@ public static async Task TryUpdateModelAsync( if (modelBindingResult.IsModelSet) { objectModelValidator.Validate( + operationBindingContext.ActionContext, operationBindingContext.ValidatorProvider, - modelState, modelBindingContext.ValidationState, modelBindingResult.Key, modelBindingResult.Model); diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs index 6e090cea58..5ea43b5d95 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs @@ -40,26 +40,26 @@ public DefaultObjectValidator( /// public void Validate( + ActionContext actionContext, IModelValidatorProvider validatorProvider, - ModelStateDictionary modelState, ValidationStateDictionary validationState, string prefix, object model) { - if (validatorProvider == null) + if (actionContext == null) { - throw new ArgumentNullException(nameof(validatorProvider)); + throw new ArgumentNullException(nameof(actionContext)); } - if (modelState == null) + if (validatorProvider == null) { - throw new ArgumentNullException(nameof(modelState)); + throw new ArgumentNullException(nameof(validatorProvider)); } var visitor = new ValidationVisitor( + actionContext, validatorProvider, _excludeFilters, - modelState, validationState); var metadata = model == null ? null : _modelMetadataProvider.GetMetadataForType(model.GetType()); diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs index 502c5beab6..9f4702950a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/IObjectModelValidator.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.Extensions.Internal; - namespace Microsoft.AspNet.Mvc.ModelBinding.Validation { /// @@ -13,16 +11,16 @@ public interface IObjectModelValidator /// /// Validates the provided object. /// + /// The associated with the current request. /// The . - /// The . /// The . May be null. /// /// The model prefix. Used to map the model object to entries in . /// /// The model object. void Validate( + ActionContext actionContext, IModelValidatorProvider validatorProvider, - ModelStateDictionary modelState, ValidationStateDictionary validationState, string prefix, object model); diff --git a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs index e232caad79..3956b60314 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs @@ -15,6 +15,7 @@ public class ValidationVisitor { private readonly IModelValidatorProvider _validatorProvider; private readonly IList _excludeFilters; + private readonly ActionContext _actionContext; private readonly ModelStateDictionary _modelState; private readonly ValidationStateDictionary _validationState; @@ -30,16 +31,21 @@ public class ValidationVisitor /// /// Creates a new . /// + /// The associated with the current request. /// The . /// The list of . - /// The . /// The . public ValidationVisitor( + ActionContext actionContext, IModelValidatorProvider validatorProvider, IList excludeFilters, - ModelStateDictionary modelState, ValidationStateDictionary validationState) { + if (actionContext == null) + { + throw new ArgumentNullException(nameof(actionContext)); + } + if (validatorProvider == null) { throw new ArgumentNullException(nameof(validatorProvider)); @@ -50,16 +56,12 @@ public ValidationVisitor( throw new ArgumentNullException(nameof(excludeFilters)); } - if (modelState == null) - { - throw new ArgumentNullException(nameof(modelState)); - } - + _actionContext = actionContext; _validatorProvider = validatorProvider; _excludeFilters = excludeFilters; - _modelState = modelState; _validationState = validationState; + _modelState = actionContext.ModelState; _currentPath = new HashSet(ReferenceEqualityComparer.Instance); } @@ -101,6 +103,7 @@ protected virtual bool ValidateNode() { var context = new ModelValidationContext() { + ActionContext = _actionContext, Container = _container, Model = _model, Metadata = _metadata, diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs index fd1c9943a4..0541e13957 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs @@ -97,7 +97,7 @@ public ModelStateDictionary ModelState { get { - return ViewData?.ModelState; + return ControllerContext?.ModelState; } } @@ -1447,7 +1447,6 @@ public virtual Task TryUpdateModelAsync( model, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), valueProvider, @@ -1488,7 +1487,6 @@ public Task TryUpdateModelAsync( model, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), new CompositeValueProvider(ControllerContext.ValueProviders), @@ -1529,7 +1527,6 @@ public Task TryUpdateModelAsync( model, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), new CompositeValueProvider(ControllerContext.ValueProviders), @@ -1578,7 +1575,6 @@ public Task TryUpdateModelAsync( model, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), valueProvider, @@ -1626,7 +1622,6 @@ public Task TryUpdateModelAsync( model, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), valueProvider, @@ -1666,7 +1661,6 @@ public virtual Task TryUpdateModelAsync( modelType, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), new CompositeValueProvider(ControllerContext.ValueProviders), @@ -1719,7 +1713,6 @@ public Task TryUpdateModelAsync( modelType, prefix, ControllerContext, - ModelState, MetadataProvider, new CompositeModelBinder(ControllerContext.ModelBinders), valueProvider, @@ -1773,8 +1766,8 @@ public virtual bool TryValidateModel( modelName); ObjectValidator.Validate( + ControllerContext, new CompositeModelValidatorProvider(ControllerContext.ValidatorProviders), - ModelState, validationState: null, prefix: prefix, model: model); diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/RemoteAttribute.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/RemoteAttribute.cs index 0dcb6ddaf8..fa407f269a 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/RemoteAttribute.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/RemoteAttribute.cs @@ -198,7 +198,8 @@ protected virtual string GetUrl(ClientModelValidationContext context) throw new ArgumentNullException(nameof(context)); } - var urlHelper = context.RequestServices.GetRequiredService(); + var services = context.ActionContext.HttpContext.RequestServices; + var urlHelper = services.GetRequiredService(); var url = urlHelper.RouteUrl(new UrlRouteContext() { RouteName = this.RouteName, diff --git a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs index 6ded798a99..0717913621 100644 --- a/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs +++ b/src/Microsoft.AspNet.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs @@ -858,9 +858,9 @@ public IEnumerable GetClientValidationRules( modelExplorer = modelExplorer ?? ExpressionMetadataProvider.FromStringExpression(expression, viewContext.ViewData, _metadataProvider); var validationContext = new ClientModelValidationContext( + viewContext, modelExplorer.Metadata, - _metadataProvider, - viewContext.HttpContext.RequestServices); + _metadataProvider); var validatorProviderContext = new ClientValidatorProviderContext(modelExplorer.Metadata); _clientModelValidatorProvider.GetValidators(validatorProviderContext); diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs index c598713847..a79f5c0342 100644 --- a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/ApiController.cs @@ -572,8 +572,8 @@ public void Validate(TEntity entity, string keyPrefix) { var validatidationState = new ValidationStateDictionary(); ObjectValidator.Validate( + ControllerContext, new CompositeModelValidatorProvider(ControllerContext.ValidatorProviders), - ModelState, validatidationState, keyPrefix, entity); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/Controllers/ControllerActionArgumentBinderTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/Controllers/ControllerActionArgumentBinderTests.cs index ae7006762d..6c8f1fc2c9 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/Controllers/ControllerActionArgumentBinderTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/Controllers/ControllerActionArgumentBinderTests.cs @@ -143,8 +143,8 @@ public async Task BindActionArgumentsAsync_CallsValidator_IfModelBinderSucceeds( var mockValidator = new Mock(MockBehavior.Strict); mockValidator .Setup(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); @@ -157,12 +157,12 @@ public async Task BindActionArgumentsAsync_CallsValidator_IfModelBinderSucceeds( // Assert mockValidator .Verify(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), - Times.Once()); + Times.Once()); } [Fact] @@ -191,8 +191,8 @@ public async Task BindActionArgumentsAsync_DoesNotCallValidator_IfModelBinderFai var mockValidator = new Mock(MockBehavior.Strict); mockValidator .Setup(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); @@ -205,8 +205,8 @@ public async Task BindActionArgumentsAsync_DoesNotCallValidator_IfModelBinderFai // Assert mockValidator .Verify(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), @@ -230,8 +230,8 @@ public async Task BindActionArgumentsAsync_CallsValidator_ForControllerPropertie var mockValidator = new Mock(MockBehavior.Strict); mockValidator .Setup(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); @@ -244,8 +244,8 @@ public async Task BindActionArgumentsAsync_CallsValidator_ForControllerPropertie // Assert mockValidator .Verify(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), @@ -277,8 +277,8 @@ public async Task BindActionArgumentsAsync_DoesNotCallValidator_ForControllerPro var mockValidator = new Mock(MockBehavior.Strict); mockValidator .Setup(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); @@ -291,8 +291,8 @@ public async Task BindActionArgumentsAsync_DoesNotCallValidator_ForControllerPro // Assert mockValidator .Verify(o => o.Validate( + It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), @@ -635,8 +635,8 @@ private static IObjectModelValidator CreateMockValidator() var mockValidator = new Mock(MockBehavior.Strict); mockValidator .Setup(o => o.Validate( - It.IsAny(), - It.IsAny(), + It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ModelBindingHelperTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ModelBindingHelperTest.cs index b6aca0985b..2d4c631195 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ModelBindingHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/ModelBindingHelperTest.cs @@ -10,6 +10,7 @@ using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Mvc.Formatters; using Microsoft.AspNet.Mvc.ModelBinding.Validation; +using Microsoft.AspNet.Routing; using Moq; using Xunit; @@ -46,7 +47,6 @@ public async Task TryUpdateModel_ReturnsFalse_IfBinderIsUnsuccessful(ModelBindin model, string.Empty, new ActionContext() { HttpContext = new DefaultHttpContext() }, - new ModelStateDictionary(), metadataProvider, GetCompositeBinder(binder.Object), Mock.Of(), @@ -75,7 +75,7 @@ public async Task TryUpdateModel_ReturnsFalse_IfModelValidationFails() new TestOptionsManager(), stringLocalizerFactory: null); var model = new MyModel(); - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null } @@ -83,12 +83,14 @@ public async Task TryUpdateModel_ReturnsFalse_IfModelValidationFails() var valueProvider = new TestValueProvider(values); var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); + var actionContext = new ActionContext() { HttpContext = new DefaultHttpContext() }; + var modelState = actionContext.ModelState; + // Act var result = await ModelBindingHelper.TryUpdateModelAsync( model, "", - new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, + actionContext, modelMetadataProvider, GetCompositeBinder(binders), valueProvider, @@ -98,7 +100,7 @@ public async Task TryUpdateModel_ReturnsFalse_IfModelValidationFails() // Assert Assert.False(result); - var error = Assert.Single(modelStateDictionary["MyProperty"].Errors); + var error = Assert.Single(modelState["MyProperty"].Errors); Assert.Equal(expectedMessage, error.ErrorMessage); } @@ -116,7 +118,7 @@ public async Task TryUpdateModel_ReturnsTrue_IfModelBindsAndValidatesSuccessfull new TestOptionsManager(), stringLocalizerFactory: null); var model = new MyModel { MyProperty = "Old-Value" }; - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null }, @@ -130,7 +132,6 @@ public async Task TryUpdateModel_ReturnsTrue_IfModelBindsAndValidatesSuccessfull model, "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, metadataProvider, GetCompositeBinder(binders), valueProvider, @@ -162,7 +163,6 @@ public async Task TryUpdateModel_UsingIncludePredicateOverload_ReturnsFalse_IfBi model, string.Empty, new ActionContext() { HttpContext = new DefaultHttpContext() }, - new ModelStateDictionary(), metadataProvider, GetCompositeBinder(binder.Object), Mock.Of(), @@ -196,8 +196,7 @@ public async Task TryUpdateModel_UsingIncludePredicateOverload_ReturnsTrue_Model IncludedProperty = "Old-IncludedPropertyValue", ExcludedProperty = "Old-ExcludedPropertyValue" }; - - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null }, @@ -218,7 +217,6 @@ public async Task TryUpdateModel_UsingIncludePredicateOverload_ReturnsTrue_Model model, "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, metadataProvider, GetCompositeBinder(binders), valueProvider, @@ -252,7 +250,6 @@ public async Task TryUpdateModel_UsingIncludeExpressionOverload_ReturnsFalse_IfB model, string.Empty, new ActionContext() { HttpContext = new DefaultHttpContext() }, - new ModelStateDictionary(), metadataProvider, GetCompositeBinder(binder.Object), Mock.Of(), @@ -287,8 +284,7 @@ public async Task TryUpdateModel_UsingIncludeExpressionOverload_ReturnsTrue_Mode IncludedProperty = "Old-IncludedPropertyValue", ExcludedProperty = "Old-ExcludedPropertyValue" }; - - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null }, @@ -305,7 +301,6 @@ public async Task TryUpdateModel_UsingIncludeExpressionOverload_ReturnsTrue_Mode model, "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, TestModelMetadataProvider.CreateDefaultProvider(), GetCompositeBinder(binders), valueProvider, @@ -341,8 +336,7 @@ public async Task TryUpdateModel_UsingDefaultIncludeOverload_IncludesAllProperti IncludedProperty = "Old-IncludedPropertyValue", ExcludedProperty = "Old-ExcludedPropertyValue" }; - - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null }, @@ -359,7 +353,6 @@ public async Task TryUpdateModel_UsingDefaultIncludeOverload_IncludesAllProperti model, "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, metadataProvider, GetCompositeBinder(binders), valueProvider, @@ -514,7 +507,6 @@ public async Task TryUpdateModelNonGeneric_PredicateOverload_ReturnsFalse_IfBind model.GetType(), prefix: "", actionContext: new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelState: new ModelStateDictionary(), metadataProvider: metadataProvider, modelBinder: GetCompositeBinder(binder.Object), valueProvider: Mock.Of(), @@ -549,8 +541,7 @@ public async Task TryUpdateModelNonGeneric_PredicateOverload_ReturnsTrue_ModelBi IncludedProperty = "Old-IncludedPropertyValue", ExcludedProperty = "Old-ExcludedPropertyValue" }; - - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null }, @@ -573,7 +564,6 @@ public async Task TryUpdateModelNonGeneric_PredicateOverload_ReturnsTrue_ModelBi model.GetType(), "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, metadataProvider, GetCompositeBinder(binders), valueProvider, @@ -610,7 +600,6 @@ public async Task TryUpdateModelNonGeneric_ModelTypeOverload_ReturnsFalse_IfBind modelType: model.GetType(), prefix: "", actionContext: new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelState: new ModelStateDictionary(), metadataProvider: metadataProvider, modelBinder: GetCompositeBinder(binder.Object), valueProvider: Mock.Of(), @@ -637,7 +626,7 @@ public async Task TryUpdateModelNonGeneric_ModelTypeOverload_ReturnsTrue_IfModel new TestOptionsManager(), stringLocalizerFactory: null); var model = new MyModel { MyProperty = "Old-Value" }; - var modelStateDictionary = new ModelStateDictionary(); + var values = new Dictionary { { "", null }, @@ -652,7 +641,6 @@ public async Task TryUpdateModelNonGeneric_ModelTypeOverload_ReturnsTrue_IfModel model.GetType(), "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - modelStateDictionary, TestModelMetadataProvider.CreateDefaultProvider(), GetCompositeBinder(binders), valueProvider, @@ -687,7 +675,6 @@ public async Task TryUpdataModel_ModelTypeDifferentFromModel_Throws() typeof(User), "", new ActionContext() { HttpContext = new DefaultHttpContext() }, - new ModelStateDictionary(), metadataProvider, GetCompositeBinder(binder.Object), Mock.Of(), diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/DefaultObjectValidatorTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/DefaultObjectValidatorTests.cs index ec70897c61..c63b5f99ff 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/DefaultObjectValidatorTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ModelBinding/Validation/DefaultObjectValidatorTests.cs @@ -23,7 +23,8 @@ public void Validate_SimpleValueType_Valid_WithPrefix() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -34,7 +35,7 @@ public void Validate_SimpleValueType_Valid_WithPrefix() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert AssertKeysEqual(modelState, "parameter"); @@ -49,7 +50,8 @@ public void Validate_SimpleReferenceType_Valid_WithPrefix() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -60,7 +62,7 @@ public void Validate_SimpleReferenceType_Valid_WithPrefix() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.True(modelState.IsValid); @@ -76,7 +78,8 @@ public void Validate_SimpleType_MaxErrorsReached() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -89,7 +92,7 @@ public void Validate_SimpleType_MaxErrorsReached() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.False(modelState.IsValid); @@ -105,7 +108,8 @@ public void Validate_SimpleType_SuppressValidation() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -116,7 +120,7 @@ public void Validate_SimpleType_SuppressValidation() validationState.Add(model, new ValidationStateEntry() { Key = "parameter", SuppressValidation = true }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.True(modelState.IsValid); @@ -133,7 +137,8 @@ public void Validate_ComplexValueType_Valid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -145,7 +150,7 @@ public void Validate_ComplexValueType_Valid() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.True(modelState.IsValid); @@ -165,7 +170,8 @@ public void Validate_ComplexReferenceType_Valid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -177,7 +183,7 @@ public void Validate_ComplexReferenceType_Valid() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.True(modelState.IsValid); @@ -197,7 +203,8 @@ public void Validate_ComplexReferenceType_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -207,7 +214,7 @@ public void Validate_ComplexReferenceType_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = string.Empty }); // Act - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); // Assert Assert.False(modelState.IsValid); @@ -229,7 +236,8 @@ public void Validate_ComplexType_SuppressValidation() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -250,7 +258,7 @@ public void Validate_ComplexType_SuppressValidation() }); // Act - validator.Validate(validatorProvider, modelState, validationState, "person", model); + validator.Validate(actionContext, validatorProvider, validationState, "person", model); // Assert Assert.True(modelState.IsValid); @@ -271,7 +279,8 @@ public void Validate_ComplexReferenceType_Invalid_MultipleErrorsOnProperty() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -282,7 +291,7 @@ public void Validate_ComplexReferenceType_Invalid_MultipleErrorsOnProperty() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.False(modelState.IsValid); @@ -303,7 +312,8 @@ public void Validate_ComplexReferenceType_Invalid_MultipleErrorsOnProperty_Empty { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -314,7 +324,7 @@ public void Validate_ComplexReferenceType_Invalid_MultipleErrorsOnProperty_Empty validationState.Add(model, new ValidationStateEntry() { Key = string.Empty }); // Act - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); // Assert Assert.False(modelState.IsValid); @@ -335,7 +345,8 @@ public void Validate_NestedComplexReferenceType_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -346,7 +357,7 @@ public void Validate_NestedComplexReferenceType_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = string.Empty }); // Act - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); // Assert Assert.False(modelState.IsValid); @@ -379,7 +390,8 @@ public void Validate_ComplexType_IValidatableObject_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -391,7 +403,7 @@ public void Validate_ComplexType_IValidatableObject_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.False(modelState.IsValid); @@ -424,7 +436,8 @@ public void Validate_ComplexType_FieldsAreIgnored_Valid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -435,7 +448,7 @@ public void Validate_ComplexType_FieldsAreIgnored_Valid() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.True(modelState.IsValid); @@ -452,7 +465,8 @@ public void Validate_ComplexType_CyclesNotFollowed_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -466,7 +480,7 @@ public void Validate_ComplexType_CyclesNotFollowed_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.False(modelState.IsValid); @@ -487,7 +501,8 @@ public void Validate_ComplexType_ShortCircuit_WhenMaxErrorCountIsSet() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(typeof(string)); @@ -506,7 +521,7 @@ public void Validate_ComplexType_ShortCircuit_WhenMaxErrorCountIsSet() validationState.Add(model, new ValidationStateEntry() { Key = "user", }); // Act - validator.Validate(validatorProvider, modelState, validationState, "user", model); + validator.Validate(actionContext, validatorProvider, validationState, "user", model); // Assert Assert.False(modelState.IsValid); @@ -524,7 +539,8 @@ public void Validate_CollectionType_ArrayOfSimpleType_Valid_DefaultKeyPattern() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -536,7 +552,7 @@ public void Validate_CollectionType_ArrayOfSimpleType_Valid_DefaultKeyPattern() validationState.Add(model, new ValidationStateEntry() { Key = "parameter" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "parameter", model); + validator.Validate(actionContext, validatorProvider, validationState, "parameter", model); // Assert Assert.True(modelState.IsValid); @@ -557,7 +573,8 @@ public void Validate_CollectionType_ArrayOfComplexType_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -567,7 +584,7 @@ public void Validate_CollectionType_ArrayOfComplexType_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = string.Empty }); // Act - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); // Assert Assert.False(modelState.IsValid); @@ -600,7 +617,8 @@ public void Validate_CollectionType_ListOfComplexType_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -610,7 +628,7 @@ public void Validate_CollectionType_ListOfComplexType_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = string.Empty }); // Act - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); // Assert Assert.False(modelState.IsValid); @@ -648,7 +666,8 @@ public void Validate_IndexedCollectionTypes_Valid(object model, Type type) { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(new SimpleTypesExcludeFilter()); @@ -665,7 +684,7 @@ public void Validate_IndexedCollectionTypes_Valid(object model, Type type) }); // Act - validator.Validate(validatorProvider, modelState, validationState, "items", model); + validator.Validate(actionContext, validatorProvider, validationState, "items", model); // Assert Assert.True(modelState.IsValid); @@ -689,7 +708,8 @@ public void Validate_CollectionType_DictionaryOfSimpleType_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(new SimpleTypesExcludeFilter()); @@ -707,7 +727,7 @@ public void Validate_CollectionType_DictionaryOfSimpleType_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = "items" }); // Act - validator.Validate(validatorProvider, modelState, validationState, "items", model); + validator.Validate(actionContext, validatorProvider, validationState, "items", model); // Assert Assert.True(modelState.IsValid); @@ -736,7 +756,8 @@ public void Validate_CollectionType_DictionaryOfComplexType_Invalid() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -748,7 +769,7 @@ public void Validate_CollectionType_DictionaryOfComplexType_Invalid() validationState.Add(model, new ValidationStateEntry() { Key = string.Empty }); // Act - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); // Assert Assert.False(modelState.IsValid); @@ -796,7 +817,8 @@ public void Validate_DoesntCatchExceptions_FromPropertyAccessors() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -808,7 +830,7 @@ public void Validate_DoesntCatchExceptions_FromPropertyAccessors() typeof(InvalidTimeZoneException), () => { - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); }); } @@ -818,7 +840,8 @@ public void Validate_DoesNotUseOverridden_GetHashCodeOrEquals() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(); @@ -830,7 +853,7 @@ public void Validate_DoesNotUseOverridden_GetHashCodeOrEquals() }; // Act & Assert (does not throw) - validator.Validate(validatorProvider, modelState, validationState, string.Empty, model); + validator.Validate(actionContext, validatorProvider, validationState, string.Empty, model); } [Fact] @@ -838,7 +861,8 @@ public void Validate_ForExcludedType_PropertiesMarkedAsSkipped() { // Arrange var validatorProvider = CreateValidatorProvider(); - var modelState = new ModelStateDictionary(); + var actionContext = new ActionContext(); + var modelState = actionContext.ModelState; var validationState = new ValidationStateDictionary(); var validator = CreateValidator(typeof(User)); @@ -855,7 +879,7 @@ public void Validate_ForExcludedType_PropertiesMarkedAsSkipped() validationState.Add(model, new ValidationStateEntry() { Key = "user", }); // Act - validator.Validate(validatorProvider, modelState, validationState, "user", model); + validator.Validate(actionContext, validatorProvider, validationState, "user", model); // Assert Assert.Equal(ModelValidationState.Valid, modelState.ValidationState); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/CompareAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/CompareAttributeAdapterTest.cs index f1d0522c86..407efdaf7d 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/CompareAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/CompareAttributeAdapterTest.cs @@ -22,10 +22,8 @@ public void ClientRulesWithCompareAttribute_ErrorMessageUsesDisplayName() var attribute = new CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - - var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, metadataProvider); // Act var rules = adapter.GetClientValidationRules(context); @@ -46,12 +44,13 @@ public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); + var attribute = new CompareAttribute("OtherProperty"); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, metadataProvider); + // Act var rules = adapter.GetClientValidationRules(context); @@ -69,15 +68,16 @@ public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride() // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty( typeof(PropertyNameModel), "MyProperty"); + var attribute = new CompareAttribute("OtherProperty") { ErrorMessage = "Hello '{0}', goodbye '{1}'." }; - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, metadataProvider); + // Act var rules = adapter.GetClientValidationRules(context); @@ -94,16 +94,17 @@ public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() // Arrange var metadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = metadataProvider.GetMetadataForProperty(typeof(PropertyNameModel), "MyProperty"); + var attribute = new CompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(DataAnnotations.Test.Resources), }; - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, metadataProvider, requestServices); var adapter = new CompareAttributeAdapter(attribute, stringLocalizer: null); + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, metadataProvider); + // Act var rules = adapter.GetClientValidationRules(context); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs index 190f2d34ba..f87506fc20 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MaxLengthAttributeAdapterTest.cs @@ -19,11 +19,12 @@ public void ClientRulesWithMaxLengthAttribute() // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var attribute = new MaxLengthAttribute(10); var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); @@ -45,11 +46,12 @@ public void ClientRulesWithMaxLengthAttributeAndCustomMessage() var message = "{0} must be at most {1}"; var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), propertyName); + var attribute = new MaxLengthAttribute(5) { ErrorMessage = message }; var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); @@ -69,18 +71,17 @@ public void ClientRulesWithMaxLengthAttribute_StringLocalizer_ReturnsLocalizedEr // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var errorKey = metadata.GetDisplayName(); var attribute = new MaxLengthAttribute(10); attribute.ErrorMessage = errorKey; - var localizedString = new LocalizedString(errorKey, "Longueur est invalide"); var stringLocalizer = new Mock(); stringLocalizer.Setup(s => s[errorKey]).Returns(localizedString); - var adapter = new MaxLengthAttributeAdapter(attribute, stringLocalizer.Object); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MinLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MinLengthAttributeAdapterTest.cs index 05539d8f45..710fd73308 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MinLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/MinLengthAttributeAdapterTest.cs @@ -3,7 +3,6 @@ using System.ComponentModel.DataAnnotations; using Microsoft.AspNet.Testing; -using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation @@ -17,11 +16,12 @@ public void ClientRulesWithMinLengthAttribute() // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var attribute = new MinLengthAttribute(6); var adapter = new MinLengthAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); @@ -43,11 +43,12 @@ public void ClientRulesWithMinLengthAttributeAndCustomMessage() var message = "Array must have at least {1} items."; var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), propertyName); + var attribute = new MinLengthAttribute(2) { ErrorMessage = message }; var adapter = new MinLengthAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/NumericClientModelValidatorTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/NumericClientModelValidatorTest.cs index 9e7eb32b5f..e6d1c0e658 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/NumericClientModelValidatorTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/NumericClientModelValidatorTest.cs @@ -3,7 +3,6 @@ using System.ComponentModel.DataAnnotations; using Microsoft.AspNet.Testing; -using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation @@ -17,10 +16,12 @@ public void ClientRulesWithCorrectValidationTypeAndErrorMessage() // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(TypeWithNumericProperty), "Id"); + var adapter = new NumericClientModelValidator(); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); + var expectedMessage = "The field DisplayId must be a number."; // Act diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RangeAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RangeAttributeAdapterTest.cs index 75f2431029..1238d6c3cb 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RangeAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RangeAttributeAdapterTest.cs @@ -3,7 +3,6 @@ using System.ComponentModel.DataAnnotations; using Microsoft.AspNet.Testing; -using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation @@ -17,11 +16,12 @@ public void GetClientValidationRules_ReturnsValidationParameters() // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var attribute = new RangeAttribute(typeof(decimal), "0", "100"); var adapter = new RangeAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RequiredAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RequiredAttributeAdapterTest.cs index 4a40cd818f..c41079082f 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RequiredAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/RequiredAttributeAdapterTest.cs @@ -3,7 +3,6 @@ using System.ComponentModel.DataAnnotations; using Microsoft.AspNet.Testing; -using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation @@ -18,11 +17,12 @@ public void GetClientValidationRules_ReturnsValidationParameters() var expected = ValidationAttributeUtil.GetRequiredErrorMessage("Length"); var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var attribute = new RequiredAttribute(); var adapter = new RequiredAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); diff --git a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/StringLengthAttributeAdapterTest.cs b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/StringLengthAttributeAdapterTest.cs index 98d1b93aa6..a77f3f97bd 100644 --- a/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/StringLengthAttributeAdapterTest.cs +++ b/test/Microsoft.AspNet.Mvc.DataAnnotations.Test/StringLengthAttributeAdapterTest.cs @@ -3,7 +3,6 @@ using System.ComponentModel.DataAnnotations; using Microsoft.AspNet.Testing; -using Microsoft.Extensions.DependencyInjection; using Xunit; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation @@ -17,11 +16,12 @@ public void GetClientValidationRules_WithMaxLength_ReturnsValidationParameters() // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var attribute = new StringLengthAttribute(8); var adapter = new StringLengthAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); @@ -41,11 +41,12 @@ public void GetClientValidationRules_WithMinAndMaxLength_ReturnsValidationParame // Arrange var provider = TestModelMetadataProvider.CreateDefaultProvider(); var metadata = provider.GetMetadataForProperty(typeof(string), "Length"); + var attribute = new StringLengthAttribute(10) { MinimumLength = 3 }; var adapter = new StringLengthAttributeAdapter(attribute, stringLocalizer: null); - var serviceCollection = new ServiceCollection(); - var requestServices = serviceCollection.BuildServiceProvider(); - var context = new ClientModelValidationContext(metadata, provider, requestServices); + + var actionContext = new ActionContext(); + var context = new ClientModelValidationContext(actionContext, metadata, provider); // Act var rules = adapter.GetClientValidationRules(context); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ActionParametersIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ActionParametersIntegrationTest.cs index 4af250af04..86e3886589 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ActionParametersIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ActionParametersIntegrationTest.cs @@ -53,11 +53,11 @@ public async Task ActionParameter_NonSettableCollectionModel_EmptyPrefix_GetsBou request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person3(); // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -104,10 +104,10 @@ public async Task ActionParameter_ReadOnlyCollectionModel_EmptyPrefix_DoesNotGet request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -157,10 +157,10 @@ public async Task ActionParameter_SettableArrayModel_EmptyPrefix_GetsBound() request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person4(); // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -209,9 +209,9 @@ public async Task ActionParameter_NonSettableArrayModel_EmptyPrefix_DoesNotGetBo request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -254,10 +254,10 @@ public async Task ActionParameter_NonSettableCollectionModel_WithPrefix_GetsBoun request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -303,10 +303,10 @@ public async Task ActionParameter_ReadOnlyCollectionModel_WithPrefix_DoesNotGetB request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -355,10 +355,10 @@ public async Task ActionParameter_SettableArrayModel_WithPrefix_GetsBound() request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -405,10 +405,10 @@ public async Task ActionParameter_NonSettableArrayModel_WithPrefix_DoesNotGetBou request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ArrayModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ArrayModelBinderIntegrationTest.cs index 2e70102134..eb8cae2e03 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ArrayModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ArrayModelBinderIntegrationTest.cs @@ -28,10 +28,10 @@ public async Task ArrayModelBinder_BindsArrayOfSimpleType_WithPrefix_Success() request.QueryString = new QueryString("?parameter[0]=10¶meter[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -72,10 +72,10 @@ public async Task ArrayModelBinder_BindsArrayOfSimpleType_WithExplicitPrefix_Suc request.QueryString = new QueryString("?prefix[0]=10&prefix[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -112,10 +112,10 @@ public async Task ArrayModelBinder_BindsArrayOfSimpleType_EmptyPrefix_Success() request.QueryString = new QueryString("?[0]=10&[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -152,10 +152,10 @@ public async Task ArrayModelBinder_BindsArrayOfSimpleType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -187,10 +187,10 @@ public async Task ArrayModelBinder_BindsArrayOfComplexType_WithPrefix_Success() request.QueryString = new QueryString("?parameter[0].Name=bill¶meter[1].Name=lang"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -232,10 +232,10 @@ public async Task ArrayModelBinder_BindsArrayOfComplexType_WithExplicitPrefix_Su request.QueryString = new QueryString("?prefix[0].Name=bill&prefix[1].Name=lang"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -273,10 +273,10 @@ public async Task ArrayModelBinder_BindsArrayOfComplexType_EmptyPrefix_Success() request.QueryString = new QueryString("?[0].Name=bill&[1].Name=lang"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -314,10 +314,10 @@ public async Task ArrayModelBinder_BindsArrayOfComplexType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/BinderTypeBasedModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/BinderTypeBasedModelBinderIntegrationTest.cs index 717e5db2e1..6dddaba2aa 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/BinderTypeBasedModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/BinderTypeBasedModelBinderIntegrationTest.cs @@ -31,10 +31,10 @@ public async Task BindParameter_WithModelBinderType_NullData_ReturnsNull() // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -69,10 +69,10 @@ public async Task BindParameter_WithModelBinderType_NoData() // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.Equal(ModelBindingResult.NoResult, modelBindingResult); @@ -104,10 +104,10 @@ public async Task BindParameter_WithModelBinderType_NonGreedy_NoData() // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -140,10 +140,10 @@ public async Task BindParameter_WithOutModelBinderType() // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -175,10 +175,10 @@ public async Task BindParameter_WithData_WithPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -218,10 +218,10 @@ public async Task BindProperty_WithData_EmptyPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -259,10 +259,10 @@ public async Task BindProperty_WithData_WithPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/BodyValidationIntegrationTests.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/BodyValidationIntegrationTests.cs index f52d1e7566..237aa7e5b2 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/BodyValidationIntegrationTests.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/BodyValidationIntegrationTests.cs @@ -47,10 +47,10 @@ public async Task FromBodyAndRequiredOnProperty_EmptyBody_AddsModelStateError() request.ContentType = "application/json"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -88,10 +88,10 @@ public async Task FromBodyOnActionParameter_EmptyBody_BindsToNullValue() }); var httpContext = operationContext.HttpContext; - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -132,10 +132,10 @@ public async Task FromBodyAndRequiredOnValueTypeProperty_EmptyBody_JsonFormatter request.ContentType = "application/json"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -191,10 +191,10 @@ public async Task FromBodyAndRequiredOnInnerValueTypeProperty_NotBound_JsonForma request.ContentType = "application/json"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -234,10 +234,10 @@ public async Task FromBodyWithInvalidPropertyData_JsonFormatterAddsModelError() request.ContentType = "application/json"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -304,10 +304,10 @@ public async Task FromBodyOnTopLevelProperty_RequiredOnSubProperty_AddsModelStat request.ContentType = "application/json"; }); var httpContext = operationContext.HttpContext; - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -363,10 +363,10 @@ public async Task FromBodyOnProperty_Succeeds_IgnoresRequiredOnValueTypeSubPrope request.Body = new MemoryStream(Encoding.UTF8.GetBytes(inputText)); request.ContentType = "application/json"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ByteArrayModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ByteArrayModelBinderIntegrationTest.cs index f41b856929..4546003b78 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ByteArrayModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ByteArrayModelBinderIntegrationTest.cs @@ -50,10 +50,10 @@ public async Task BindProperty_WithData_GetsBound(bool fallBackScenario) { request.QueryString = QueryString.Create(queryStringKey, value); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -95,10 +95,10 @@ public async Task BindParameter_NoData_DoesNotGetBound() // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -134,10 +134,10 @@ public async Task BindParameter_WithData_GetsBound() request.QueryString = QueryString.Create("CustomParameter", value); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/CancellationTokenModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/CancellationTokenModelBinderIntegrationTest.cs index daead4ad7a..444136025f 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/CancellationTokenModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/CancellationTokenModelBinderIntegrationTest.cs @@ -37,10 +37,10 @@ public async Task BindProperty_WithData_WithPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -71,10 +71,10 @@ public async Task BindProperty_WithData_WithEmptyPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -108,10 +108,10 @@ public async Task BindParameter_WithData_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/CollectionModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/CollectionModelBinderIntegrationTest.cs index c2d13a5a8b..c64a3c8f60 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/CollectionModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/CollectionModelBinderIntegrationTest.cs @@ -37,10 +37,10 @@ public async Task CollectionModelBinder_BindsListOfSimpleType_WithPrefix_Success request.QueryString = new QueryString("?parameter[0]=10¶meter[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -83,10 +83,10 @@ public async Task CollectionModelBinder_BindsListOfSimpleType_WithExplicitPrefix request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -117,10 +117,10 @@ public async Task CollectionModelBinder_BindsCollectionOfSimpleType_EmptyPrefix_ request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -149,10 +149,10 @@ public async Task CollectionModelBinder_BindsListOfSimpleType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -188,10 +188,10 @@ public async Task CollectionModelBinder_BindsListOfComplexType_ImpliedPrefix_Suc request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -227,10 +227,10 @@ public async Task CollectionModelBinder_BindsListOfComplexType_ExplicitPrefix_Su request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -260,10 +260,10 @@ public async Task CollectionModelBinder_BindsListOfComplexType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -298,10 +298,10 @@ public async Task CollectionModelBinder_BindsListOfComplexType_WithRequiredPrope request.QueryString = new QueryString("?parameter[0].Id=10¶meter[1].Id=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -357,10 +357,10 @@ public async Task CollectionModelBinder_BindsListOfComplexType_WithRequiredPrope request.QueryString = new QueryString("?prefix[0].Id=10&prefix[1].Id=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -408,10 +408,10 @@ public async Task CollectionModelBinder_BindsCollectionOfComplexType_WithRequire request.QueryString = new QueryString("?[0].Id=10&[1].Id=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -460,10 +460,10 @@ public async Task CollectionModelBinder_BindsListOfSimpleType_WithIndex_Success( new QueryString("?parameter.index=low¶meter.index=high¶meter[low]=10¶meter[high]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -503,10 +503,10 @@ public async Task CollectionModelBinder_BindsCollectionOfComplexType_WithRequire request.QueryString = new QueryString("?index=low&index=high&[high].Id=11&[low].Id=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -554,10 +554,10 @@ public async Task CollectionModelBinder_BindsListOfComplexType_WithRequiredPrope request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -604,10 +604,10 @@ public async Task CollectionModelBinder_UsesCustomIndexes() request.ContentType = "application/x-www-form-urlencoded"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -661,10 +661,10 @@ public async Task CollectionModelBinder_UsesCustomIndexes_AddsErrorsWithCorrectK request.ContentType = "application/x-www-form-urlencoded"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -700,10 +700,10 @@ public async Task CollectionModelBinder_BindsCollectionOfComplexType_ImpliedPref request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -920,14 +920,14 @@ public async Task CollectionModelBinder_BindsParameterToExpectedType( }; var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); - var modelState = new ModelStateDictionary(); var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.Form = new FormCollection(formContent); }); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/DictionaryModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/DictionaryModelBinderIntegrationTest.cs index dd5c38aae5..c2de2c5210 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/DictionaryModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/DictionaryModelBinderIntegrationTest.cs @@ -32,10 +32,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithPrefixAn request.QueryString = new QueryString("?parameter[0].Key=key0¶meter[0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -72,10 +72,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithPrefixAn request.QueryString = new QueryString("?parameter[key0]=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -110,10 +110,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithIndex_Su new QueryString("?parameter.index=low¶meter[low].Key=key0¶meter[low].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -163,10 +163,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_WithExplicit request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -198,10 +198,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_EmptyPrefix_ request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -230,10 +230,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfSimpleType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -291,10 +291,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_ImpliedPref request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -331,10 +331,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_ExplicitPre request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -370,10 +370,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_ImpliedPref request.QueryString = new QueryString(queryString); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -410,10 +410,10 @@ public async Task DictionaryModelBinder_BindsDictionaryOfComplexType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -517,14 +517,14 @@ public async Task DictionaryModelBinder_BindsParameterToExpectedType( }; var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(); - var modelState = new ModelStateDictionary(); var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request => { request.QueryString = new QueryString(queryString); }); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/FormCollectionModelBindingIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/FormCollectionModelBindingIntegrationTest.cs index 98330119c3..047630d9ba 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/FormCollectionModelBindingIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/FormCollectionModelBindingIntegrationTest.cs @@ -49,10 +49,10 @@ public async Task BindProperty_WithData_WithEmptyPrefix_GetsBound() UpdateRequest(request, data, "Address.File"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -99,10 +99,10 @@ public async Task BindParameter_WithData_GetsBound() UpdateRequest(request, data, "CustomParameter"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult @@ -139,10 +139,10 @@ public async Task BindParameter_NoData_BindsWithEmptyCollection() // No data is passed. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/FormFileModelBindingIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/FormFileModelBindingIntegrationTest.cs index 4de7d5a61a..64f66a3815 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/FormFileModelBindingIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/FormFileModelBindingIntegrationTest.cs @@ -49,10 +49,10 @@ public async Task BindProperty_WithData_WithEmptyPrefix_GetsBound() UpdateRequest(request, data, "Address.File"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -100,10 +100,10 @@ public async Task BindParameter_WithData_GetsBound() UpdateRequest(request, data, "CustomParameter"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult @@ -146,10 +146,10 @@ public async Task BindParameter_NoData_DoesNotGetBound() var operationContext = ModelBindingTestHelper.GetOperationBindingContext( request => UpdateRequest(request, data: null, name: null)); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.Equal(ModelBindingResult.NoResult, modelBindingResult); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/GenericModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/GenericModelBinderIntegrationTest.cs index 2209e4bd3a..044adfeb7d 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/GenericModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/GenericModelBinderIntegrationTest.cs @@ -37,10 +37,10 @@ public async Task GenericModelBinder_BindsCollection_ElementTypeFromGreedyModelB request.QueryString = new QueryString("?parameter.index=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -75,10 +75,10 @@ public async Task GenericModelBinder_BindsCollection_ElementTypeFromGreedyModelB request.QueryString = new QueryString("?index=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -114,10 +114,10 @@ public async Task GenericModelBinder_BindsCollection_ElementTypeFromGreedyModelB request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -183,10 +183,10 @@ public async Task GenericModelBinder_BindsCollection_ElementTypeUsesGreedyModelB request => request.QueryString = new QueryString("?parameter.index=0"), options => options.ModelBinders.Add(new AddressBinder())); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -217,10 +217,10 @@ public async Task GenericModelBinder_BindsCollection_ElementTypeUsesGreedyBindin var operationContext = ModelBindingTestHelper.GetOperationBindingContext( request => request.QueryString = new QueryString("?parameter.index=0")); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -252,10 +252,10 @@ public async Task GenericModelBinder_BindsArrayOfDictionary_WithPrefix_Success() request.QueryString = new QueryString("?parameter[0][0].Key=key0¶meter[0][0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -297,10 +297,10 @@ public async Task GenericModelBinder_BindsArrayOfDictionary_EmptyPrefix_Success( request.QueryString = new QueryString("?[0][0].Key=key0&[0][0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -342,10 +342,10 @@ public async Task GenericModelBinder_BindsArrayOfDictionary_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -377,10 +377,10 @@ public async Task GenericModelBinder_BindsCollectionOfKeyValuePair_WithPrefix_Su request.QueryString = new QueryString("?parameter[0].Key=key0¶meter[0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -421,10 +421,10 @@ public async Task GenericModelBinder_BindsCollectionOfKeyValuePair_EmptyPrefix_S request.QueryString = new QueryString("?[0].Key=key0&[0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -465,10 +465,10 @@ public async Task GenericModelBinder_BindsCollectionOfKeyValuePair_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -501,10 +501,10 @@ public async Task GenericModelBinder_BindsDictionaryOfList_WithPrefix_Success() "?parameter[0].Key=key0¶meter[0].Value[0]=10¶meter[0].Value[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -549,10 +549,10 @@ public async Task GenericModelBinder_BindsDictionaryOfList_EmptyPrefix_Success() request.QueryString = new QueryString("?[0].Key=key0&[0].Value[0]=10&[0].Value[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -597,10 +597,10 @@ public async Task GenericModelBinder_BindsDictionaryOfList_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/HeaderModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/HeaderModelBinderIntegrationTest.cs index ba70da0401..02ce26bf80 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/HeaderModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/HeaderModelBinderIntegrationTest.cs @@ -43,10 +43,10 @@ public async Task BindPropertyFromHeader_NoData_UsesFullPathAsKeyForModelStateEr // Do not add any headers. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -86,10 +86,10 @@ public async Task BindPropertyFromHeader_WithPrefix_GetsBound() request.Headers.Add("Header", new[] { "someValue" }); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -128,10 +128,10 @@ public async Task BindPropertyFromHeader_WithData_WithEmptyPrefix_GetsBound() var operationContext = ModelBindingTestHelper.GetOperationBindingContext( request => request.Headers.Add("Header", new[] { "someValue" })); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -190,10 +190,10 @@ public async Task BindParameterFromHeader_WithData_WithPrefix_ModelGetsBound(Typ // Do not add any headers. var httpContext = operationContext.HttpContext; - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/KeyValuePairModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/KeyValuePairModelBinderIntegrationTest.cs index ceb01a9305..4b523d75f4 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/KeyValuePairModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/KeyValuePairModelBinderIntegrationTest.cs @@ -30,10 +30,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfSimpleType_WithPref request.QueryString = new QueryString("?parameter.Key=key0¶meter.Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -68,10 +68,10 @@ public async Task KeyValuePairModelBinder_SimpleTypes_WithNoKey_AddsError() { request.QueryString = new QueryString("?parameter.Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.False(modelBindingResult.IsModelSet); @@ -116,10 +116,10 @@ public async Task KeyValuePairModelBinder_SimpleTypes_WithNoKey_AndCustomizedMes { request.QueryString = new QueryString("?parameter.Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.False(modelBindingResult.IsModelSet); @@ -153,10 +153,10 @@ public async Task KeyValuePairModelBinder_SimpleTypes_WithNoValue_AddsError() { request.QueryString = new QueryString("?parameter.Key=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.False(modelBindingResult.IsModelSet); @@ -201,10 +201,10 @@ public async Task KeyValuePairModelBinder_SimpleTypes_WithNoValue_AndCustomizedM { request.QueryString = new QueryString("?parameter.Key=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.False(modelBindingResult.IsModelSet); @@ -244,10 +244,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfSimpleType_WithExpl request.QueryString = new QueryString("?prefix.Key=key0&prefix.Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -284,10 +284,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfSimpleType_EmptyPre request.QueryString = new QueryString("?Key=key0&Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -324,10 +324,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfSimpleType_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -360,10 +360,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfComplexType_WithPre request.QueryString = new QueryString("?parameter.Key=key0¶meter.Value.Id=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -405,10 +405,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfComplexType_WithExp request.QueryString = new QueryString("?prefix.Key=key0&prefix.Value.Id=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -446,10 +446,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfComplexType_EmptyPr request.QueryString = new QueryString("?Key=key0&Value.Id=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -487,10 +487,10 @@ public async Task KeyValuePairModelBinder_BindsKeyValuePairOfComplexType_NoData( request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ModelPrefixSelectionIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ModelPrefixSelectionIntegrationTest.cs index ad8d7a2c1b..705308bcfc 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ModelPrefixSelectionIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ModelPrefixSelectionIntegrationTest.cs @@ -45,10 +45,10 @@ public async Task ComplexModel_PrefixSelected_ByValueProvider() }); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -95,10 +95,10 @@ public async Task ComplexModel_PrefixSelected_ByValueProviderValue_WithFilteredV }); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -135,10 +135,10 @@ public async Task ComplexModel_EmptyPrefixSelected_NoMatchingValueProviderValue( request.QueryString = new QueryString("?Name="); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -185,10 +185,10 @@ public async Task ComplexModel_EmptyPrefixSelected_NoMatchingValueProviderValue_ }); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -232,10 +232,10 @@ public async Task ComplexModel_EmptyPrefixSelected_NoMatchingValueProviderValue_ }); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/MutableObjectModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/MutableObjectModelBinderIntegrationTest.cs index 52aeffc176..95fb22806e 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/MutableObjectModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/MutableObjectModelBinderIntegrationTest.cs @@ -66,10 +66,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_W SetJsonBodyContent(request, AddressBodyContent); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -111,10 +111,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_W SetJsonBodyContent(request, AddressBodyContent); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -156,10 +156,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_W request.ContentType = "application/json"; }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -201,10 +201,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_W SetJsonBodyContent(request, AddressBodyContent); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -242,10 +242,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_W SetJsonBodyContent(request, AddressBodyContent); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -290,10 +290,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithByteArrayModelBin new QueryString("?parameter.Customer.Name=bill¶meter.Customer.Token=" + ByteArrayEncoded); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -333,10 +333,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithByteArrayModelBin request.QueryString = new QueryString("?Customer.Name=bill&Customer.Token=" + ByteArrayEncoded); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -376,10 +376,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithByteArrayModelBin request.QueryString = new QueryString("?parameter.Customer.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -430,10 +430,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithFormFileModelBind SetFormFileBodyContent(request, "Hello, World!", "parameter.Customer.Documents"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -474,10 +474,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithFormFileModelBind SetFormFileBodyContent(request, "Hello, World!", "Customer.Documents"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -519,10 +519,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithFormFileModelBind // Deliberately leaving out any form data. }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -565,10 +565,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithFormFileModelBind SetFormFileBodyContent(request, "Hello, World!", "parameter.Customer.Documents"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -606,10 +606,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithFormFileModelBind SetFormFileBodyContent(request, "Hello, World!", "parameter.Customer.Documents"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -647,10 +647,10 @@ public async Task MutableObjectModelBinder_BindsArrayProperty_WithPrefix_Success new QueryString("?parameter.Name=bill¶meter.ProductIds[0]=10¶meter.ProductIds[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -693,10 +693,10 @@ public async Task MutableObjectModelBinder_BindsArrayProperty_EmptyPrefix_Succes request.QueryString = new QueryString("?Name=bill&ProductIds[0]=10&ProductIds[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -739,10 +739,10 @@ public async Task MutableObjectModelBinder_BindsArrayProperty_NoCollectionData() request.QueryString = new QueryString("?parameter.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -777,10 +777,10 @@ public async Task MutableObjectModelBinder_BindsArrayProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -819,10 +819,10 @@ public async Task MutableObjectModelBinder_BindsListProperty_WithPrefix_Success( new QueryString("?parameter.Name=bill¶meter.ProductIds[0]=10¶meter.ProductIds[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -865,10 +865,10 @@ public async Task MutableObjectModelBinder_BindsListProperty_EmptyPrefix_Success request.QueryString = new QueryString("?Name=bill&ProductIds[0]=10&ProductIds[1]=11"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -911,10 +911,10 @@ public async Task MutableObjectModelBinder_BindsListProperty_NoCollectionData() request.QueryString = new QueryString("?parameter.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -949,10 +949,10 @@ public async Task MutableObjectModelBinder_BindsListProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -991,10 +991,10 @@ public async Task MutableObjectModelBinder_BindsDictionaryProperty_WithPrefix_Su new QueryString("?parameter.Name=bill¶meter.ProductIds[0].Key=key0¶meter.ProductIds[0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1037,10 +1037,10 @@ public async Task MutableObjectModelBinder_BindsDictionaryProperty_EmptyPrefix_S request.QueryString = new QueryString("?Name=bill&ProductIds[0].Key=key0&ProductIds[0].Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1083,10 +1083,10 @@ public async Task MutableObjectModelBinder_BindsDictionaryProperty_NoCollectionD request.QueryString = new QueryString("?parameter.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1121,10 +1121,10 @@ public async Task MutableObjectModelBinder_BindsDictionaryProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1163,10 +1163,10 @@ public async Task MutableObjectModelBinder_BindsKeyValuePairProperty_WithPrefix_ new QueryString("?parameter.Name=bill¶meter.ProductId.Key=key0¶meter.ProductId.Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1209,10 +1209,10 @@ public async Task MutableObjectModelBinder_BindsKeyValuePairProperty_EmptyPrefix request.QueryString = new QueryString("?Name=bill&ProductId.Key=key0&ProductId.Value=10"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1255,10 +1255,10 @@ public async Task MutableObjectModelBinder_BindsKeyValuePairProperty_NoCollectio request.QueryString = new QueryString("?parameter.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1293,10 +1293,10 @@ public async Task MutableObjectModelBinder_BindsKeyValuePairProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1341,10 +1341,10 @@ public async Task MutableObjectModelBinder_BindsNestedPOCO_WithAllGreedyBoundPro SetJsonBodyContent(request, AddressBodyContent); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1389,10 +1389,10 @@ public async Task MutableObjectModelBinder_WithRequiredComplexProperty_NoData_Ge // No Data var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1436,10 +1436,10 @@ public async Task MutableObjectModelBinder_WithBindRequired_NoData_AndCustomized // No Data var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1488,10 +1488,10 @@ public async Task MutableObjectModelBinder_WithNestedRequiredProperty_WithPartia request.QueryString = new QueryString("?parameter.Customer.Id=123"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1533,10 +1533,10 @@ public async Task MutableObjectModelBinder_WithNestedRequiredProperty_WithData_E request.QueryString = new QueryString("?Customer.Id=123"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1582,10 +1582,10 @@ public async Task MutableObjectModelBinder_WithNestedRequiredProperty_WithData_C request.QueryString = new QueryString("?customParameter.Customer.Id=123"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1633,10 +1633,10 @@ public async Task MutableObjectModelBinder_WithRequiredProperty_NoData_GetsError request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1676,10 +1676,10 @@ public async Task MutableObjectModelBinder_WithRequiredProperty_NoData_CustomPre request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1715,10 +1715,10 @@ public async Task MutableObjectModelBinder_WithRequiredProperty_WithData_EmptyPr request.QueryString = new QueryString("?ProductName=abc"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1758,10 +1758,10 @@ public async Task MutableObjectModelBinder_WithRequiredCollectionProperty_NoData request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1801,10 +1801,10 @@ public async Task MutableObjectModelBinder_WithRequiredCollectionProperty_NoData request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1840,10 +1840,10 @@ public async Task MutableObjectModelBinder_WithRequiredCollectionProperty_WithDa request.QueryString = new QueryString("?OrderIds[0]=123"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1884,10 +1884,10 @@ public async Task MutableObjectModelBinder_BindsPOCO_TypeConvertedPropertyNonCon request.QueryString = new QueryString("?parameter.ProductId="); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1928,10 +1928,10 @@ public async Task MutableObjectModelBinder_BindsPOCO_TypeConvertedPropertyWithNo request.QueryString = new QueryString("?parameter.ProductId"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs index 220f3c70b3..4d1e1f3b64 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ServicesModelBinderIntegrationTest.cs @@ -32,10 +32,10 @@ public async Task BindParameterFromService_WithData_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -69,10 +69,10 @@ public async Task BindParameterFromService_NoPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult @@ -105,10 +105,10 @@ public async Task BindEnumerableParameterFromService_NoPrefix_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult @@ -141,10 +141,10 @@ public async Task BindEnumerableParameterFromService_NoService_GetsBound() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult @@ -177,11 +177,10 @@ public async Task BindParameterFromService_NoService_Throws() }; var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); // Act & Assert var exception = await Assert.ThrowsAsync( - () => argumentBinder.BindModelAsync(parameter, modelState, operationContext)); + () => argumentBinder.BindModelAsync(parameter, operationContext)); Assert.Contains(typeof(IActionResult).FullName, exception.Message); } } diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/SimpleTypeModelBinderIntegrationTest.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/SimpleTypeModelBinderIntegrationTest.cs index 3f60ba7669..996199d354 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/SimpleTypeModelBinderIntegrationTest.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/SimpleTypeModelBinderIntegrationTest.cs @@ -42,10 +42,10 @@ public async Task BindProperty_WithData_WithPrefix_GetsBound() request.QueryString = QueryString.Create("CustomParameter.Address.Zip", "1"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -86,10 +86,10 @@ public async Task BindProperty_WithData_WithEmptyPrefix_GetsBound() request.QueryString = QueryString.Create("Address.Zip", "1"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -131,10 +131,10 @@ public async Task BindParameter_WithData_GetsBound() request.QueryString = QueryString.Create("Parameter1", "someValue"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -175,10 +175,10 @@ public async Task BindParameter_WithMultipleValues_GetsBoundToFirstValue() request.QueryString = new QueryString("?Parameter1=someValue&Parameter1=otherValue"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -219,10 +219,10 @@ public async Task BindParameter_NonConvertableValue_GetsError() request.QueryString = QueryString.Create("Parameter1", "abcd"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -273,10 +273,10 @@ public async Task BindParameter_WithEmptyData_DoesNotBind(Type parameterType) { request.QueryString = QueryString.Create("Parameter1", " "); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -326,10 +326,10 @@ public async Task BindParameter_WithEmptyData_AndPerTypeMessage_AddsGivenMessage { request.QueryString = QueryString.Create("Parameter1", string.Empty); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert // ModelBindingResult @@ -369,10 +369,10 @@ public async Task BindParameter_WithEmptyData_BindsMutableAndNullableObjects(Typ { request.QueryString = QueryString.Create("Parameter1", string.Empty); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -407,10 +407,10 @@ public async Task BindParameter_NoData_DoesNotGetBound() // No Data. var operationContext = ModelBindingTestHelper.GetOperationBindingContext(); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert @@ -461,10 +461,10 @@ public async Task BindParameter_FromFormData_BindsCorrectly(Dictionary { new Person1 @@ -138,7 +138,7 @@ public async Task TryUpdateModel_TopLevelCollection_EmptyPrefix_BindsAfterCleari }; // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -175,7 +175,7 @@ public async Task TryUpdateModel_NestedPoco_EmptyPrefix_DoesNotTrounceUnboundVal request.QueryString = QueryString.Create("Address.Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person1 { Name = "Joe", @@ -188,7 +188,7 @@ public async Task TryUpdateModel_NestedPoco_EmptyPrefix_DoesNotTrounceUnboundVal var oldModel = model; // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -230,11 +230,11 @@ public async Task TryUpdateModel_SettableCollectionModel_EmptyPrefix_CreatesColl request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person2(); // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -271,7 +271,7 @@ public async Task TryUpdateModel_SettableCollectionModel_EmptyPrefix_MaintainsCo request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person2 { Address = new List
(), @@ -279,7 +279,7 @@ public async Task TryUpdateModel_SettableCollectionModel_EmptyPrefix_MaintainsCo var collection = model.Address; // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -327,7 +327,7 @@ public async Task TryUpdateModel_NonSettableCollectionModel_EmptyPrefix_GetsBoun request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person3 { Address = @@ -346,7 +346,7 @@ public async Task TryUpdateModel_NonSettableCollectionModel_EmptyPrefix_GetsBoun }; // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -382,11 +382,11 @@ public async Task TryUpdateModel_ReadOnlyCollectionModel_EmptyPrefix_DoesNotGetB request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person6(); // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -426,11 +426,11 @@ public async Task TryUpdateModel_SettableArrayModel_EmptyPrefix_CreatesArray() request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person4(); // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -467,7 +467,7 @@ public async Task TryUpdateModel_SettableArrayModel_EmptyPrefix_OverwritesArray( request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person4 { Address = new Address[] @@ -482,7 +482,7 @@ public async Task TryUpdateModel_SettableArrayModel_EmptyPrefix_OverwritesArray( var collection = model.Address; // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -525,11 +525,11 @@ public async Task TryUpdateModel_NonSettableArrayModel_EmptyPrefix_GetsBound() request.QueryString = QueryString.Create("Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person5(); // Act - var result = await TryUpdateModel(model, string.Empty, operationContext, modelState); + var result = await TryUpdateModel(model, string.Empty, operationContext); // Assert Assert.True(result); @@ -555,7 +555,7 @@ public async Task TryUpdateModel_ExistingModel_WithPrefix_ValuesGetOverwritten() request.QueryString = QueryString.Create("prefix.Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Address { Street = "DefaultStreet", @@ -564,7 +564,7 @@ public async Task TryUpdateModel_ExistingModel_WithPrefix_ValuesGetOverwritten() var oldModel = model; // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -595,11 +595,11 @@ public async Task TryUpdateModel_ExistingModel_WithPrefix_GetsBound() request.QueryString = QueryString.Create("prefix.Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Address(); // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -638,7 +638,7 @@ public async Task TryUpdateModel_TopLevelCollection_WithPrefix_BindsAfterClearin }); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new List { new Person1 @@ -655,7 +655,7 @@ public async Task TryUpdateModel_TopLevelCollection_WithPrefix_BindsAfterClearin }; // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -692,7 +692,7 @@ public async Task TryUpdateModel_NestedPoco_WithPrefix_DoesNotTrounceUnboundValu request.QueryString = QueryString.Create("prefix.Address.Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person1 { Name = "Joe", @@ -705,7 +705,7 @@ public async Task TryUpdateModel_NestedPoco_WithPrefix_DoesNotTrounceUnboundValu var oldModel = model; // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -742,11 +742,11 @@ public async Task TryUpdateModel_SettableCollectionModel_WithPrefix_CreatesColle request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person2(); // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -783,7 +783,7 @@ public async Task TryUpdateModel_SettableCollectionModel_WithPrefix_MaintainsCol request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person2 { Address = new List
(), @@ -791,7 +791,7 @@ public async Task TryUpdateModel_SettableCollectionModel_WithPrefix_MaintainsCol var collection = model.Address; // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -829,7 +829,7 @@ public async Task TryUpdateModel_NonSettableCollectionModel_WithPrefix_GetsBound request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person3 { Address = @@ -848,7 +848,7 @@ public async Task TryUpdateModel_NonSettableCollectionModel_WithPrefix_GetsBound }; // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -879,11 +879,11 @@ public async Task TryUpdateModel_ReadOnlyCollectionModel_WithPrefix_DoesNotGetBo request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person6(); // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -918,11 +918,11 @@ public async Task TryUpdateModel_SettableArrayModel_WithPrefix_CreatesArray() request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person4(); // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -959,7 +959,7 @@ public async Task TryUpdateModel_SettableArrayModel_WithPrefix_OverwritesArray() request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person4 { Address = new Address[] @@ -974,7 +974,7 @@ public async Task TryUpdateModel_SettableArrayModel_WithPrefix_OverwritesArray() var collection = model.Address; // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -1012,11 +1012,11 @@ public async Task TryUpdateModel_NonSettableArrayModel_WithPrefix_GetsBound() request.QueryString = QueryString.Create("prefix.Address[0].Street", "SomeStreet"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; var model = new Person5(); // Act - var result = await TryUpdateModel(model, "prefix", operationContext, modelState); + var result = await TryUpdateModel(model, "prefix", operationContext); // Assert Assert.True(result); @@ -1098,15 +1098,13 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() private Task TryUpdateModel( object model, string prefix, - OperationBindingContext operationContext, - ModelStateDictionary modelState) + OperationBindingContext operationContext) { return ModelBindingHelper.TryUpdateModelAsync( model, model.GetType(), prefix, operationContext.ActionContext, - modelState, operationContext.MetadataProvider, operationContext.ModelBinder, operationContext.ValueProvider, diff --git a/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs b/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs index 61e14ac0fc..0f525cfed0 100644 --- a/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs +++ b/test/Microsoft.AspNet.Mvc.IntegrationTests/ValidationIntegrationTests.cs @@ -37,10 +37,10 @@ public async Task Validation_RequiredAttribute_OnSimpleTypeProperty_WithData() request.QueryString = new QueryString("?parameter.CustomerName=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -74,10 +74,10 @@ public async Task Validation_RequiredAttribute_OnSimpleTypeProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -125,10 +125,10 @@ public async Task Validation_RequiredAttribute_OnPOCOProperty_WithData() request.QueryString = new QueryString("?parameter.Customer.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -163,10 +163,10 @@ public async Task Validation_RequiredAttribute_OnPOCOProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -216,10 +216,10 @@ public async Task Validation_RequiredAttribute_OnNestedSimpleTypeProperty_WithDa request.QueryString = new QueryString("?parameter.Customer.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -255,10 +255,10 @@ public async Task Validation_RequiredAttribute_OnNestedSimpleTypeProperty_NoData request.QueryString = new QueryString("?parameter.Customer.Age=17"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -308,10 +308,10 @@ public async Task Validation_RequiredAttribute_OnCollectionProperty_WithData() request.QueryString = new QueryString("?Items[0].ItemId=17"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -347,10 +347,10 @@ public async Task Validation_RequiredAttribute_OnCollectionProperty_NoData() request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -395,10 +395,10 @@ public async Task Validation_RequiredAttribute_OnPOCOPropertyOfBoundElement_With request.QueryString = new QueryString("?parameter[0].ProductId=17"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -433,10 +433,10 @@ public async Task Validation_RequiredAttribute_OnPOCOPropertyOfBoundElement_NoDa request.QueryString = new QueryString("?parameter[0].Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -481,10 +481,10 @@ public async Task Validation_StringLengthAttribute_OnPropertyOfPOCO_Valid() request.QueryString = new QueryString("?parameter.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -518,10 +518,10 @@ public async Task Validation_StringLengthAttribute_OnPropertyOfPOCO_Invalid() request.QueryString = new QueryString("?parameter.Name=billybob"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -569,10 +569,10 @@ public async Task Validation_StringLengthAttribute_OnPropertyOfNestedPOCO_Valid( request.QueryString = new QueryString("?parameter.Customer.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -606,10 +606,10 @@ public async Task Validation_StringLengthAttribute_OnPropertyOfNestedPOCO_Invali request.QueryString = new QueryString("?parameter.Customer.Name=billybob"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -646,10 +646,10 @@ public async Task Validation_StringLengthAttribute_OnPropertyOfNestedPOCO_NoData request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -704,10 +704,10 @@ public async Task Validation_CustomAttribute_OnPOCOProperty_Valid() request.QueryString = new QueryString("?parameter.Customer.Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -741,10 +741,10 @@ public async Task Validation_CustomAttribute_OnPOCOProperty_Invalid() request.QueryString = new QueryString("?parameter.Customer.Name=billybob"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -811,10 +811,10 @@ public async Task Validation_CustomAttribute_OnCollectionElement_Valid() request.QueryString = new QueryString("?parameter.Products[0].Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -848,10 +848,10 @@ public async Task Validation_CustomAttribute_OnCollectionElement_Invalid() request.QueryString = new QueryString("?parameter.Products[0].Name=billybob"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -899,10 +899,10 @@ public async Task Validation_StringLengthAttribute_OnProperyOfCollectionElement_ request.QueryString = new QueryString("?parameter[0].Name=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -936,10 +936,10 @@ public async Task Validation_StringLengthAttribute_OnProperyOfCollectionElement_ request.QueryString = new QueryString("?parameter[0].Name=billybob"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -976,10 +976,10 @@ public async Task Validation_StringLengthAttribute_OnProperyOfCollectionElement_ request.QueryString = new QueryString("?"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1016,10 +1016,10 @@ public async Task Validation_FormatException_ShowsInvalidValueMessage_OnSimpleTy request.QueryString = new QueryString("?Id=bill"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1056,10 +1056,10 @@ public async Task Validation_OverflowException_ShowsInvalidValueMessage_OnSimple request.QueryString = new QueryString("?Zip=-123"); }); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); // Assert Assert.True(modelBindingResult.IsModelSet); @@ -1134,10 +1134,10 @@ public async Task TypeBasedExclusion_ForBodyAndNonBodyBoundModels() }); var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(testOptions); - var modelState = new ModelStateDictionary(); + var modelState = operationContext.ActionContext.ModelState; // Act - var modelBindingResult = await argumentBinder.BindModelAsync(parameter, modelState, operationContext); + var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext); Assert.Equal(4, modelState.Count); Assert.Equal(0, modelState.ErrorCount); diff --git a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs index cdcab8708d..e73600fc97 100644 --- a/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.ViewFeatures.Test/RemoteAttributeTest.cs @@ -445,7 +445,15 @@ private static ClientModelValidationContext GetValidationContext(IUrlHelper urlH serviceCollection.AddSingleton(urlHelper); var serviceProvider = serviceCollection.BuildServiceProvider(); - return new ClientModelValidationContext(_metadata, _metadataProvider, serviceProvider); + var actionContext = new ActionContext() + { + HttpContext = new DefaultHttpContext() + { + RequestServices = serviceProvider, + }, + }; + + return new ClientModelValidationContext(actionContext, _metadata, _metadataProvider); } private static ClientModelValidationContext GetValidationContextWithArea(string currentArea) @@ -475,7 +483,15 @@ private static ClientModelValidationContext GetValidationContextWithArea(string serviceCollection.AddSingleton(urlHelper); serviceProvider = serviceCollection.BuildServiceProvider(); - return new ClientModelValidationContext(_metadata, _metadataProvider, serviceProvider); + var actionContext = new ActionContext() + { + HttpContext = new DefaultHttpContext() + { + RequestServices = serviceProvider, + }, + }; + + return new ClientModelValidationContext(actionContext, _metadata, _metadataProvider); } private static ClientModelValidationContext GetValidationContextWithNoController() @@ -494,9 +510,9 @@ private static ClientModelValidationContext GetValidationContextWithNoController var contextAccessor = GetContextAccessor(serviceProvider, routeData); var urlHelper = new UrlHelper(contextAccessor); serviceCollection.AddSingleton(urlHelper); - serviceProvider = serviceCollection.BuildServiceProvider(); + contextAccessor.ActionContext.HttpContext.RequestServices = serviceCollection.BuildServiceProvider(); - return new ClientModelValidationContext(_metadata, _metadataProvider, serviceProvider); + return new ClientModelValidationContext(contextAccessor.ActionContext, _metadata, _metadataProvider); } private static IRouter GetRouteCollectionWithArea(IServiceProvider serviceProvider)