当前位置: 首页>>代码示例>>C#>>正文


C# ModelMetadataProvider.GetMetadataForType方法代码示例

本文整理汇总了C#中System.Web.Http.Metadata.ModelMetadataProvider.GetMetadataForType方法的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadataProvider.GetMetadataForType方法的具体用法?C# ModelMetadataProvider.GetMetadataForType怎么用?C# ModelMetadataProvider.GetMetadataForType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.Http.Metadata.ModelMetadataProvider的用法示例。


在下文中一共展示了ModelMetadataProvider.GetMetadataForType方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Validate

		public bool Validate(object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, string keyPrefix) {
			if (type == null) {
				throw new ArgumentNullException("type");
			}

			if (metadataProvider == null) {
				throw new ArgumentNullException("metadataProvider");
			}

			if (actionContext == null) {
				throw new ArgumentNullException("actionContext");
			}

			if (model != null && MediaTypeFormatterCollection.IsTypeExcludedFromValidation(model.GetType())) {
				// no validation for some DOM like types
				return true;
			}

			ModelValidatorProvider[] validatorProviders = actionContext.GetValidatorProviders().ToArray();
			// Optimization : avoid validating the object graph if there are no validator providers
			if (validatorProviders == null || validatorProviders.Length == 0) {
				return true;
			}

			ModelMetadata metadata = metadataProvider.GetMetadataForType(() => model, type);
			ValidationContext validationContext = new ValidationContext {
				MetadataProvider = metadataProvider,
				ActionContext = actionContext,
				ModelState = actionContext.ModelState,
				Visited = new HashSet<object>(),
				KeyBuilders = new Stack<IKeyBuilder>(),
				RootPrefix = keyPrefix
			};
			return this.ValidateNodeAndChildren(metadata, validationContext, container: null);
		}
开发者ID:jango2015,项目名称:FluentValidation,代码行数:35,代码来源:FluentValidationBodyModelValidator.cs

示例2: GetModelBindingContext

        private ModelBindingContext GetModelBindingContext(ModelMetadataProvider metadataProvider, HttpActionContext actionContext)
        {
            string name = Descriptor.ParameterName;
            Type type = Descriptor.ParameterType;

            string prefix = Descriptor.Prefix;

            IValueProvider vp = CreateValueProvider(this._valueProviderFactories, actionContext);

            if (_metadataCache == null)
            {
                Interlocked.Exchange(ref _metadataCache, metadataProvider.GetMetadataForType(null, type));
            }

            ModelBindingContext ctx = new ModelBindingContext()
            {
                ModelName = prefix ?? name,
                FallbackToEmptyPrefix = prefix == null, // only fall back if prefix not specified
                ModelMetadata = _metadataCache,
                ModelState = actionContext.ModelState,
                ValueProvider = vp
            };
            
            if (_validationNodeCache == null)
            {
                Interlocked.Exchange(ref _validationNodeCache, ctx.ValidationNode);
            }
            else
            {
                ctx.ValidationNode = _validationNodeCache;
            }

            return ctx;
        }
开发者ID:acprofessionale,项目名称:aspnetwebstack,代码行数:34,代码来源:ModelBinderParameterBinding.cs

示例3: ExecuteBindingAsync

        public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            string name = Descriptor.ParameterName;
            Type type = Descriptor.ParameterType;

            string prefix = Descriptor.Prefix;

            IValueProvider vp = CreateValueProvider(this._valueProviderFactories, actionContext);

            ModelBindingContext ctx = new ModelBindingContext()
            {
                ModelName = prefix ?? name,
                FallbackToEmptyPrefix = prefix == null, // only fall back if prefix not specified
                ModelMetadata = metadataProvider.GetMetadataForType(null, type),
                ModelState = actionContext.ModelState,
                ValueProvider = vp
            };

            IModelBinder binder = this._modelBinderProvider.GetBinder(actionContext, ctx);

            bool haveResult = binder.BindModel(actionContext, ctx);
            object model = haveResult ? ctx.Model : Descriptor.DefaultValue;
            actionContext.ActionArguments.Add(name, model);

            return TaskHelpers.Completed();
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:26,代码来源:ModelBinderParameterBinding.cs

示例4: CreateModelValidationNode

        private static ModelValidationNode CreateModelValidationNode(object o, ModelMetadataProvider metadataProvider, ModelStateDictionary modelStateDictionary, string modelStateKey)
        {
            ModelMetadata metadata = metadataProvider.GetMetadataForType(() =>
            {
                return o;
            }, o.GetType());
            ModelValidationNode validationNode = new ModelValidationNode(metadata, modelStateKey);

            // for this root node, recursively add all child nodes
            HashSet<object> visited = new HashSet<object>();
            CreateModelValidationNodeRecursive(o, validationNode, metadataProvider, metadata, modelStateDictionary, modelStateKey, visited);

            return validationNode;
        }
开发者ID:marojeri,项目名称:aspnetwebstack,代码行数:14,代码来源:DataControllerValidation.cs

示例5: Validate

        /// <summary>
        /// Determines whether the <paramref name="model"/> is valid and adds any validation errors to the <paramref name="actionContext"/>'s <see cref="ModelStateDictionary"/>
        /// </summary>
        /// <param name="model">The model to be validated.</param>
        /// <param name="type">The <see cref="Type"/> to use for validation.</param>
        /// <param name="metadataProvider">The <see cref="ModelMetadataProvider"/> used to provide the model metadata.</param>
        /// <param name="actionContext">The <see cref="HttpActionContext"/> within which the model is being validated.</param>
        /// <param name="keyPrefix">The <see cref="string"/> to append to the key for any validation errors.</param>
        /// <returns><c>true</c>if <paramref name="model"/> is valid, <c>false</c> otherwise.</returns>
        public bool Validate(object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, string keyPrefix)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            if (metadataProvider == null)
            {
                throw Error.ArgumentNull("metadataProvider");
            }

            if (actionContext == null)
            {
                throw Error.ArgumentNull("actionContext");
            }

            if (model != null && !ShouldValidateType(model.GetType()))
            {
                return true;
            }

            ModelValidatorProvider[] validatorProviders = actionContext.GetValidatorProviders().ToArray();
            // Optimization : avoid validating the object graph if there are no validator providers
            if (validatorProviders == null || validatorProviders.Length == 0)
            {
                return true;
            }

            ModelMetadata metadata = metadataProvider.GetMetadataForType(() => model, type);
            ValidationContext validationContext = new ValidationContext()
            {
                MetadataProvider = metadataProvider,
                ActionContext = actionContext,
                ValidatorCache = actionContext.GetValidatorCache(),
                ModelState = actionContext.ModelState,
                Visited = new HashSet<object>(),
                KeyBuilders = new Stack<IKeyBuilder>(),
                RootPrefix = keyPrefix
            };
            return ValidateNodeAndChildren(metadata, validationContext, container: null);
        }
开发者ID:samgithub-duplicate,项目名称:aspnetwebstack,代码行数:51,代码来源:DefaultBodyModelValidator.cs

示例6: GetModelBindingContext

        private ModelBindingContext GetModelBindingContext(ModelMetadataProvider metadataProvider, HttpActionContext actionContext)
        {
            string name = Descriptor.ParameterName;
            Type type = Descriptor.ParameterType;

            string prefix = Descriptor.Prefix;

            IValueProvider vp = CompositeValueProviderFactory.GetValueProvider(actionContext, _valueProviderFactories);

            ModelBindingContext ctx = new ModelBindingContext()
            {
                ModelName = prefix ?? name,
                FallbackToEmptyPrefix = prefix == null, // only fall back if prefix not specified
                ModelMetadata = metadataProvider.GetMetadataForType(null, type),
                ModelState = actionContext.ModelState,
                ValueProvider = vp
            };

            return ctx;
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:20,代码来源:ModelBinderParameterBinding.cs

示例7: Validate

        /// <summary>
        /// Determines whether the <paramref name="model"/> is valid and adds any validation errors to the <paramref name="actionContext"/>'s <see cref="ModelStateDictionary"/>
        /// </summary>
        /// <param name="model">The model to be validated.</param>
        /// <param name="type">The <see cref="Type"/> to use for validation.</param>
        /// <param name="metadataProvider">The <see cref="ModelMetadataProvider"/> used to provide the model metadata.</param>
        /// <param name="actionContext">The <see cref="HttpActionContext"/> within which the model is being validated.</param>
        /// <param name="keyPrefix">The <see cref="string"/> to append to the key for any validation errors.</param>
        /// <returns><c>true</c>if <paramref name="model"/> is valid, <c>false</c> otherwise.</returns>
        public bool Validate(object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, string keyPrefix)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            if (metadataProvider == null)
            {
                throw Error.ArgumentNull("metadataProvider");
            }

            if (actionContext == null)
            {
                throw Error.ArgumentNull("actionContext");
            }

            ModelMetadata metadata = metadataProvider.GetMetadataForType(() => model, type);
            ValidationContext validationContext = new ValidationContext() { ActionContext = actionContext, MetadataProvider = metadataProvider, Visited = new HashSet<object>() };
            return ValidateNodeAndChildren(metadata, validationContext, container: null, prefix: keyPrefix);
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:30,代码来源:DefaultBodyModelValidator.cs

示例8: Validate

        /// <summary>
        /// Determines whether the <paramref name="model"/> is valid and adds any validation errors to the <paramref name="actionContext"/>'s <see cref="ModelStateDictionary"/>
        /// </summary>
        /// <param name="model">The model to be validated.</param>
        /// <param name="type">The <see cref="Type"/> to use for validation.</param>
        /// <param name="metadataProvider">The <see cref="ModelMetadataProvider"/> used to provide the model metadata.</param>
        /// <param name="actionContext">The <see cref="HttpActionContext"/> within which the model is being validated.</param>
        /// <param name="keyPrefix">The <see cref="string"/> to append to the key for any validation errors.</param>
        /// <returns><c>true</c>if <paramref name="model"/> is valid, <c>false</c> otherwise.</returns>
        public bool Validate(object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, string keyPrefix)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            if (metadataProvider == null)
            {
                throw Error.ArgumentNull("metadataProvider");
            }

            if (actionContext == null)
            {
                throw Error.ArgumentNull("actionContext");
            }

            if (model != null && MediaTypeFormatterCollection.IsTypeExcludedFromValidation(model.GetType()))
            {
                // no validation for some DOM like types
                return true;
            }

            IEnumerable<ModelValidatorProvider> validatorProviders = actionContext.GetValidatorProviders();
            // Optimization : avoid validating the object graph if there are no validator providers
            if (validatorProviders == null || !validatorProviders.Any())
            {
                return true;
            }

            ModelMetadata metadata = metadataProvider.GetMetadataForType(() => model, type);
            ValidationContext validationContext = new ValidationContext()
            {
                MetadataProvider = metadataProvider,
                ValidatorProviders = validatorProviders,
                ModelState = actionContext.ModelState,
                Visited = new HashSet<object>()
            };
            return ValidateNodeAndChildren(metadata, validationContext, container: null, prefix: keyPrefix);
        }
开发者ID:sanyaade-mobiledev,项目名称:aspnetwebstack-1,代码行数:49,代码来源:DefaultBodyModelValidator.cs


注:本文中的System.Web.Http.Metadata.ModelMetadataProvider.GetMetadataForType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。