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


C# HttpActionContext.GetMetadataProvider方法代码示例

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


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

示例1: ValidateObject

        internal static bool ValidateObject(object o, List<ValidationResultInfo> validationErrors, HttpActionContext actionContext)
        {
            // create a model validation node for the object
            ModelMetadataProvider metadataProvider = actionContext.GetMetadataProvider();
            string modelStateKey = String.Empty;
            ModelValidationNode validationNode = CreateModelValidationNode(o, metadataProvider, actionContext.ModelState, modelStateKey);
            validationNode.ValidateAllProperties = true;

            // add the node to model state
            ModelState modelState = new ModelState();
            modelState.Value = new ValueProviderResult(o, String.Empty, CultureInfo.CurrentCulture);
            actionContext.ModelState.Add(modelStateKey, modelState);

            // invoke validation
            validationNode.Validate(actionContext);

            if (!actionContext.ModelState.IsValid)
            {
                foreach (var modelStateItem in actionContext.ModelState)
                {
                    foreach (ModelError modelError in modelStateItem.Value.Errors)
                    {
                        validationErrors.Add(new ValidationResultInfo(modelError.ErrorMessage, new string[] { modelStateItem.Key }));
                    }
                }
            }

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

示例2: BindModel

        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (!CanBindType(bindingContext.ModelType))
            {
                return false;
            }
            if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
            {
                return false;
            }

            bindingContext.Model = Activator.CreateInstance(bindingContext.ModelType);
            ComplexModelDto dto = new ComplexModelDto(bindingContext.ModelMetadata, bindingContext.PropertyMetadata.Values);
            ModelBindingContext subContext = new ModelBindingContext(bindingContext)
            {
                ModelMetadata = actionContext.GetMetadataProvider().GetMetadataForType(() => dto, typeof(ComplexModelDto)),
                ModelName = bindingContext.ModelName
            };
            actionContext.Bind(subContext);

            foreach (KeyValuePair<ModelMetadata, ComplexModelDtoResult> item in dto.Results)
            {
                ModelMetadata propertyMetadata = item.Key;
                ComplexModelDtoResult dtoResult = item.Value;
                if (dtoResult != null)
                {
                    PropertyInfo propertyInfo = bindingContext.ModelType.GetProperty(propertyMetadata.PropertyName);
                    if (propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(bindingContext.Model, dtoResult.Model);
                    }
                }
            }
            return true;
        }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:35,代码来源:MyMutableObjectModelBinder.cs

示例3: Get

 public HttpResponseMessage Get()
 {
     Address address = new Address
     {
         Province = "江苏省",
         City = "苏州市",
         District = "工业园区",
         Street = "星湖街328号"
     };
     Contact contact = new Contact
     {
         Name = "张三",
         PhoneNo = "123456789",
         EmailAddress = "[email protected]",
         Address = address
     };
     IBodyModelValidator validator = new DefaultBodyModelValidator();
     HttpActionContext actionContext = new HttpActionContext
     {
         ControllerContext = this.ControllerContext
     };
     ModelMetadataProvider metadataProvider =actionContext.GetMetadataProvider();
     validator.Validate(contact, typeof(Contact), metadataProvider,actionContext, "contact");
     return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,actionContext.ModelState);
 }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:25,代码来源:DemoController.cs

示例4: CreateAndPopulateDto

        private ComplexModelDto CreateAndPopulateDto(HttpActionContext actionContext, ModelBindingContext bindingContext, IEnumerable<ModelMetadata> propertyMetadatas)
        {
            ModelMetadataProvider metadataProvider = MetadataProvider ?? actionContext.GetMetadataProvider();

            // create a DTO and call into the DTO binder
            ComplexModelDto originalDto = new ComplexModelDto(bindingContext.ModelMetadata, propertyMetadatas);
            ModelBindingContext dtoBindingContext = new ModelBindingContext(bindingContext)
            {
                ModelMetadata = metadataProvider.GetMetadataForType(() => originalDto, typeof(ComplexModelDto)),
                ModelName = bindingContext.ModelName
            };

            IModelBinder dtoBinder = actionContext.GetBinder(dtoBindingContext);
            dtoBinder.BindModel(actionContext, dtoBindingContext);
            return (ComplexModelDto)dtoBindingContext.Model;
        }
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:16,代码来源:MutableObjectModelBinder.cs

示例5: OnActionExecuting

        /// <summary>
        /// Pick out validation errors and turn these into a suitable exception structure
        /// </summary>
        /// <param name="actionContext">Action Context</param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            ModelStateDictionary modelState = actionContext.ModelState;

            if (modelState.IsValid)
            {
                // Only perform the FluentValidation if we've not already failed validation earlier on
                IDependencyScope scope = actionContext.Request.GetDependencyScope();
                IFluentValidatorProvider mvp = scope.GetService(typeof(IFluentValidatorProvider)) as IFluentValidatorProvider;
                mvp = mvp ?? new FluentValidatorProvider(new ScopedValidatorFactory());

                if (mvp != null)
                {
                    ModelMetadataProvider metadataProvider = actionContext.GetMetadataProvider();

                    foreach (KeyValuePair<string, object> argument in actionContext.ActionArguments)
                    {
                        if (argument.Value != null && !argument.Value.GetType().IsSimpleType())
                        {
                            ModelMetadata metadata = metadataProvider.GetMetadataForType(
                                    () => argument.Value,
                                    argument.Value.GetType()
                                );

                            InternalValidationContext validationContext = new InternalValidationContext()
                            {
                                MetadataProvider = metadataProvider,
                                ActionContext = actionContext,
                                ModelState = actionContext.ModelState,
                                Visited = new HashSet<object>(),
                                KeyBuilders = new Stack<IKeyBuilder>(),
                                RootPrefix = String.Empty,
                                Provider = mvp,
                                Scope = scope
                            };

                            ValidateNodeAndChildren(metadata, validationContext, null);
                        }
                    }
                }
            }
        }
开发者ID:MosaixSoftware,项目名称:public,代码行数:46,代码来源:FluentValidationActionFilter.cs

示例6: ValidateProperties

        private void ValidateProperties(HttpActionContext actionContext)
        {
            // Based off CompositeModelValidator.
            ModelStateDictionary modelState = actionContext.ModelState;

            // DevDiv Bugs #227802 - Caching problem in ModelMetadata requires us to manually regenerate
            // the ModelMetadata.
            object model = ModelMetadata.Model;
            ModelMetadata updatedMetadata = actionContext.GetMetadataProvider().GetMetadataForType(() => model, ModelMetadata.ModelType);

            foreach (ModelMetadata propertyMetadata in updatedMetadata.Properties)
            {
                // Only want to add errors to ModelState if something doesn't already exist for the property node,
                // else we could end up with duplicate or irrelevant error messages.
                string propertyKeyRoot = ModelBindingHelper.CreatePropertyModelName(ModelStateKey, propertyMetadata.PropertyName);

                if (modelState.IsValidField(propertyKeyRoot))
                {
                    foreach (ModelValidator propertyValidator in actionContext.GetValidators(propertyMetadata))
                    {
                        foreach (ModelValidationResult propertyResult in propertyValidator.Validate(propertyMetadata, model))
                        {
                            string thisErrorKey = ModelBindingHelper.CreatePropertyModelName(propertyKeyRoot, propertyResult.MemberName);
                            modelState.AddModelError(thisErrorKey, propertyResult.Message);
                        }
                    }
                }
            }
        }
开发者ID:reza899,项目名称:aspnetwebstack,代码行数:29,代码来源:ModelValidationNode.cs


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