當前位置: 首頁>>代碼示例>>C#>>正文


C# Metadata.ModelMetadata類代碼示例

本文整理匯總了C#中System.Web.Http.Metadata.ModelMetadata的典型用法代碼示例。如果您正苦於以下問題:C# ModelMetadata類的具體用法?C# ModelMetadata怎麽用?C# ModelMetadata使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ModelMetadata類屬於System.Web.Http.Metadata命名空間,在下文中一共展示了ModelMetadata類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Validate

            public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
            {
                bool propertiesValid = true;

                foreach (ModelMetadata propertyMetadata in metadata.Properties)
                {
                    foreach (ModelValidator propertyValidator in propertyMetadata.GetValidators(ValidatorProviders))
                    {
                        foreach (ModelValidationResult propertyResult in propertyValidator.Validate(metadata, container))
                        {
                            propertiesValid = false;
                            yield return new ModelValidationResult
                            {
                                MemberName = ModelBindingHelper.CreatePropertyModelName(propertyMetadata.PropertyName, propertyResult.MemberName),
                                Message = propertyResult.Message
                            };
                        }
                    }
                }

                if (propertiesValid)
                {
                    foreach (ModelValidator typeValidator in metadata.GetValidators(ValidatorProviders))
                    {
                        foreach (ModelValidationResult typeResult in typeValidator.Validate(metadata, container))
                        {
                            yield return typeResult;
                        }
                    }
                }
            }
開發者ID:KevMoore,項目名稱:aspnetwebstack,代碼行數:31,代碼來源:ModelValidator.cs

示例2: GetValidators

        public IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata)
        {
            // If metadata is for a property then containerType != null && propertyName != null
            // If metadata is for a type then containerType == null && propertyName == null, so we have to use modelType for the cache key.
            Type typeForCache = metadata.ContainerType ?? metadata.ModelType;
            Tuple<Type, string> cacheKey = Tuple.Create(typeForCache, metadata.PropertyName);

            ModelValidator[] validators;
            if (!TryGetValue(cacheKey, out validators))
            {
                _cacheLock.EnterWriteLock();
                try
                {
                    // Check the cache again in case the value was computed and added to the cache while we were waiting on the write lock
                    if (!_validatorCache.TryGetValue(cacheKey, out validators))
                    {
                        // Compute validators
                        validators = metadata.GetValidators(_validatorProviders.Value).ToArray();
                        _validatorCache.Add(cacheKey, validators);
                    }
                }
                finally
                {
                    _cacheLock.ExitWriteLock();
                }
            }
            return validators;
        }
開發者ID:chrissimon-au,項目名稱:aspnetwebstack,代碼行數:28,代碼來源:ModelValidatorCache.cs

示例3: Validate

        public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
        {
            if (metadata.Model != null)
            {
                var selector = _customizations.ToValidatorSelector();
                //var interceptor = _customizations.GetInterceptor() ?? (_validator as IValidatorInterceptor);
                var context = new ValidationContext(metadata.Model, new PropertyChain(), selector);

                //if (interceptor != null)
                //{
                //    // Allow the user to provide a customized context
                //    // However, if they return null then just use the original context.
                //    context = interceptor.BeforeMvcValidation(ControllerContext, context) ?? context;
                //}

                var result = _validator.Validate(context);

                //if (interceptor != null)
                //{
                //    // allow the user to provice a custom collection of failures, which could be empty.
                //    // However, if they return null then use the original collection of failures.
                //    result = interceptor.AfterMvcValidation(ControllerContext, context, result) ?? result;
                //}

                if (!result.IsValid)
                {
                    return ConvertValidationResultToModelValidationResults(result);
                }
            }
            return Enumerable.Empty<ModelValidationResult>();
        }
開發者ID:danludwig,項目名稱:danludwig,代碼行數:31,代碼來源:FluentValidationHttpModelValidator.cs

示例4: DefaultValues

        public void DefaultValues()
        {
            // Arrange
            Mock<ModelMetadataProvider> provider = new Mock<ModelMetadataProvider>();

            // Act
            ModelMetadata metadata = new ModelMetadata(provider.Object, typeof(Exception), () => "model", typeof(string), "propertyName");

            // Assert
            Assert.Equal(typeof(Exception), metadata.ContainerType);
            Assert.True(metadata.ConvertEmptyStringToNull);
            Assert.Null(metadata.DataTypeName);
            Assert.Null(metadata.Description);
            Assert.Null(metadata.DisplayFormatString);
            Assert.Null(metadata.DisplayName);
            Assert.Null(metadata.EditFormatString);
            Assert.False(metadata.HideSurroundingHtml);
            Assert.Equal("model", metadata.Model);
            Assert.Equal(typeof(string), metadata.ModelType);
            Assert.Null(metadata.NullDisplayText);
            Assert.Equal(10000, metadata.Order);
            Assert.Equal("propertyName", metadata.PropertyName);
            Assert.False(metadata.IsReadOnly);
            Assert.True(metadata.RequestValidationEnabled);
            Assert.Null(metadata.ShortDisplayName);
            Assert.True(metadata.ShowForDisplay);
            Assert.True(metadata.ShowForEdit);
            Assert.Null(metadata.TemplateHint);
            Assert.Null(metadata.Watermark);
        }
開發者ID:haoduotnt,項目名稱:aspnetwebstack,代碼行數:30,代碼來源:ModelMetadataTest.cs

示例5: GetValidators

        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<ModelValidatorProvider> validatorProviders, IEnumerable<Attribute> attributes)
        {
                List<ModelValidator> results = new List<ModelValidator>();

                // Produce a validator for each validation attribute we find
                foreach (ValidationAttribute attribute in attributes.OfType<ValidationAttribute>())
                {
                    DataAnnotationsModelValidationFactory factory;
                    if (!AttributeFactories.TryGetValue(attribute.GetType(), out factory))
                    {
                        factory = DefaultAttributeFactory;
                    }
                    results.Add(factory(metadata, validatorProviders, attribute));
                }

                // Produce a validator if the type supports IValidatableObject
                if (typeof(IValidatableObject).IsAssignableFrom(metadata.ModelType))
                {
                    DataAnnotationsValidatableObjectAdapterFactory factory;
                    if (!ValidatableFactories.TryGetValue(metadata.ModelType, out factory))
                    {
                        factory = DefaultValidatableFactory;
                    }
                    results.Add(factory(metadata, validatorProviders));
                }

                return results;
            }
開發者ID:haoduotnt,項目名稱:aspnetwebstack,代碼行數:28,代碼來源:DataAnnotationsModelValidatorProvider.cs

示例6: Validate

        public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
        {
            if (metadata.Model is string)
            {
                var dirty = (string) metadata.Model;
                var sanitized = Sanitizer.GetSafeHtmlFragment(dirty);
                if (!dirty.Equals(sanitized))
                {
                    // Revert HTML encoded special characters
                    sanitized = sanitized.Replace("&lt;", "<");
                    sanitized = sanitized.Replace("&gt;", ">");
                    sanitized = sanitized.Replace("&amp;", "&");
                    sanitized = sanitized.Replace("&quot;", "\"");

                    if (!dirty.Equals(sanitized))
                    {
                        yield return new ModelValidationResult
                        {
                            MemberName = string.Empty,
                            Message = "A potentially dangerous value was detected from the client."
                        };
                    }
                }
            }
        }
開發者ID:rmichela,項目名稱:WebApiXss,代碼行數:25,代碼來源:AntiXssModelValidator.cs

示例7: Validate

 public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
 {
     if (metadata.Model is string)
     {
         return ValidateString((string)metadata.Model, metadata.PropertyName);
     }
     return Enumerable.Empty<ModelValidationResult>();
 }
開發者ID:rmichela,項目名稱:WebApiXss,代碼行數:8,代碼來源:AspNetXssModelValidator.cs

示例8: GetValidators

 public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<ModelValidatorProvider> validatorProviders)
 {
     if (IsValidatingProperty(metadata))
     {
         return GetValidatorsForProperty(metadata, validatorProviders, ValidatorFactory.GetValidator(metadata.ContainerType));
     }
     return GetValidatorsForModel(
         //metadata,
         validatorProviders, ValidatorFactory.GetValidator(metadata.ModelType));
 }
開發者ID:danludwig,項目名稱:danludwig,代碼行數:10,代碼來源:FluentValidationHttpModelValidatorProvider.cs

示例9: GetValidators

        /// <summary>
        /// Gets the collection of registered <see cref="ModelValidator"/> instances.
        /// </summary>
        /// <param name="actionContext">The context.</param>
        /// <param name="metadata">The metadata.</param>
        /// <returns>A collection of registered <see cref="ModelValidator"/> instances.</returns>
        public static IEnumerable<ModelValidator> GetValidators(this HttpActionContext actionContext, ModelMetadata metadata)
        {
            if (actionContext == null)
            {
                throw Error.ArgumentNull("actionContext");
            }

            IEnumerable<ModelValidatorProvider> validatorProviders = GetValidatorProviders(actionContext);
            return validatorProviders.SelectMany(provider => provider.GetValidators(metadata, validatorProviders));
        }
開發者ID:JokerMisfits,項目名稱:linux-packaging-mono,代碼行數:16,代碼來源:HttpActionContextExtensions.cs

示例10: GetValidators

        /// <summary>
        /// Gets the validators.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="validatorProviders">The validator providers.</param>
        /// <returns>
        /// The validators returned by the default validator providers.
        /// </returns>
        public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, IEnumerable<ModelValidatorProvider> validatorProviders)
        {
            var validators = this.defaultModelValidatorProviders.SelectMany(provider => provider.GetValidators(metadata, validatorProviders)).ToList();
            foreach (var modelValidator in validators)
            {
                this.kernel.Inject(modelValidator);
            }

            return validators;
        }
開發者ID:remogloor,項目名稱:Ninject.Web.WebApi,代碼行數:18,代碼來源:NinjectDefaultModelValidatorProvider.cs

示例11: TypeConversionErrorMessageProvider_DefaultValue

        public void TypeConversionErrorMessageProvider_DefaultValue()
        {
            // Arrange
            ModelMetadata metadata = new ModelMetadata(new Mock<ModelMetadataProvider>().Object, null, null, typeof(int), "SomePropertyName");

            // Act
            string errorString = ModelBinderConfig.TypeConversionErrorMessageProvider(null, metadata, "some incoming value");

            // Assert
            Assert.Equal("The value 'some incoming value' is not valid for SomePropertyName.", errorString);
        }
開發者ID:haoduotnt,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:ModelBinderConfigTest.cs

示例12: GetValidators

        /// <summary>
        /// Gets a list of validators.
        /// </summary>
        /// <param name="metadata">The metadata.</param>
        /// <param name="actionContext">The context.</param>
        /// <param name="attributes">The list of validation attributes.</param>
        /// <returns>A list of validators.</returns>
        protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, HttpActionContext actionContext, IEnumerable<Attribute> attributes)
        {
            var validators = base.GetValidators(metadata, actionContext, attributes);
            foreach (var modelValidator in validators.OfType<DataAnnotationsModelValidator>())
            {
                var attribute = this.getAttributeMethodInfo.Invoke(modelValidator, new object[0]);
                this.kernel.Inject(attribute);
            }

            return validators;
        }
開發者ID:pseabury,項目名稱:Ninject.Web.WebApi,代碼行數:18,代碼來源:NinjectDataAnnotationsModelValidatorProvider.cs

示例13: Validate

		public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container) {
		    if (metadata.Model == null) return Enumerable.Empty<ModelValidationResult>();
		    var selector = new DefaultValidatorSelector();
		    var context = new ValidationContext(metadata.Model, new PropertyChain(), selector);

		    var result = validator.Validate(context);

		    return !result.IsValid 
                ? ConvertValidationResultToModelValidationResults(result) 
                : Enumerable.Empty<ModelValidationResult>();
		}
開發者ID:maca88,項目名稱:FluentValidation.WebApi2,代碼行數:11,代碼來源:FluentValidationModelValidator.cs

示例14: ValueRequiredErrorMessageProvider_DefaultValue

        public void ValueRequiredErrorMessageProvider_DefaultValue()
        {
            // Arrange
            ModelMetadata metadata = new ModelMetadata(new Mock<ModelMetadataProvider>().Object, null, null, typeof(int), "SomePropertyName");

            // Act
            string errorString = ModelBinderConfig.ValueRequiredErrorMessageProvider(null, metadata, "some incoming value");

            // Assert
            Assert.Equal("A value is required.", errorString);
        }
開發者ID:Vizzini,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:ModelBinderConfigTest.cs

示例15: GetValidatorsForProperty

        private IEnumerable<ModelValidator> GetValidatorsForProperty(ModelMetadata metadata, IEnumerable<ModelValidatorProvider> validatorProviders)
        {
            ICustomTypeDescriptor typeDescriptor = GetTypeDescriptor(metadata.ContainerType);
            PropertyDescriptor property = typeDescriptor.GetProperties().Find(metadata.PropertyName, true);
            if (property == null)
            {
                throw Error.Argument("metadata", SRResources.Common_PropertyNotFound, metadata.ContainerType, metadata.PropertyName);
            }

            return GetValidators(metadata, validatorProviders, property.Attributes.OfType<Attribute>());
        }
開發者ID:KevMoore,項目名稱:aspnetwebstack,代碼行數:11,代碼來源:AssociatedValidatorProvider.cs


注:本文中的System.Web.Http.Metadata.ModelMetadata類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。