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


C# HttpActionContext.Bind方法代码示例

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


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

示例1: 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

示例2: Get

        public Tuple<string[], string[]> Get()
        {
            HttpActionContext actionContext = new HttpActionContext
            {
                ControllerContext = this.ControllerContext
            };
            ModelMetadataProvider metadataProvider = this.Configuration.Services.GetModelMetadataProvider();
            ModelMetadata metadata = metadataProvider.GetMetadataForType(null, typeof(DemoModel));
            IValueProvider valueProvider = new CompositeValueProviderFactory(this.Configuration.Services.GetValueProviderFactories()).GetValueProvider(actionContext);
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = metadata,
                ValueProvider = valueProvider,
                ModelState = actionContext.ModelState
            };
            actionContext.Bind(bindingContext);

            //验证之前的错误消息
            string[] errorMessages1 = actionContext.ModelState.SelectMany(
                item => item.Value.Errors.Select(
                error => error.ErrorMessage)).ToArray();

            //验证之前的错误消息
            bindingContext.ValidationNode.Validate(actionContext);
            string[] errorMessages2 = actionContext.ModelState.SelectMany(
                item => item.Value.Errors.Select(
                error => error.ErrorMessage)).ToArray();
            return new Tuple<string[], string[]>(errorMessages1, errorMessages2);
        }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:29,代码来源:DemoController.cs

示例3: BindModel

 public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
 {
     ComplexModelDto dto = bindingContext.Model as ComplexModelDto;
     if (null == dto)
     {
         return false;
     }
     foreach (ModelMetadata property in dto.PropertyMetadata)
     {
         ModelBindingContext subContext = new ModelBindingContext(bindingContext)
         {
             ModelMetadata = property,
             ModelName = ModelNameBuilder.CreatePropertyModelName(bindingContext.ModelName, property.PropertyName)
         };
         if (actionContext.Bind(subContext))
         {
             dto.Results[property] = new ComplexModelDtoResult(subContext.Model, subContext.ValidationNode);
         }
     }
     return true;
 }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:21,代码来源:MyComplexModelDtoModelBinder.cs

示例4: BindModel

        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            ModelBindingHelper.ValidateBindingContext(bindingContext, typeof(ComplexModelDto), false /* allowNullModel */);

            ComplexModelDto dto = (ComplexModelDto)bindingContext.Model;
            foreach (ModelMetadata propertyMetadata in dto.PropertyMetadata)
            {
                ModelBindingContext propertyBindingContext = new ModelBindingContext(bindingContext)
                {
                    ModelMetadata = propertyMetadata,
                    ModelName = ModelBindingHelper.CreatePropertyModelName(bindingContext.ModelName, propertyMetadata.PropertyName)
                };

                // bind and propagate the values
                // If we can't bind, then leave the result missing (don't add a null).
                if (actionContext.Bind(propertyBindingContext))                
                {
                    dto.Results[propertyMetadata] = new ComplexModelDtoResult(propertyBindingContext.Model, propertyBindingContext.ValidationNode);
                }                
            }

            return true;
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:23,代码来源:ComplexModelDtoModelBinder.cs

示例5: 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
            };

            actionContext.Bind(dtoBindingContext);
            return (ComplexModelDto)dtoBindingContext.Model;
        }
开发者ID:marojeri,项目名称:aspnetwebstack,代码行数:15,代码来源:MutableObjectModelBinder.cs

示例6: TryBind

 private bool TryBind(HttpActionContext actionContext, ModelBindingContext bindingContext)
 {
     return actionContext.Bind(bindingContext, Binders);
 }
开发者ID:reza899,项目名称:aspnetwebstack,代码行数:4,代码来源:CompositeModelBinder.cs


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