• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# ModelBinding.EmptyModelMetadataProvider类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider的典型用法代码示例。如果您正苦于以下问题:C# EmptyModelMetadataProvider类的具体用法?C# EmptyModelMetadataProvider怎么用?C# EmptyModelMetadataProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



EmptyModelMetadataProvider类属于Microsoft.AspNet.Mvc.ModelBinding命名空间,在下文中一共展示了EmptyModelMetadataProvider类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: TryUpdateModel_ReturnsFalse_IfBinderReturnsNull

        public async Task TryUpdateModel_ReturnsFalse_IfBinderReturnsNull()
        {
            // Arrange
            var metadataProvider = new EmptyModelMetadataProvider();

            var binder = new Mock<IModelBinder>();
            binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
                  .Returns(Task.FromResult<ModelBindingResult>(null));
            var model = new MyModel();

            // Act
            var result = await ModelBindingHelper.TryUpdateModelAsync(
                model,
                null,
                Mock.Of<HttpContext>(),
                new ModelStateDictionary(),
                metadataProvider,
                GetCompositeBinder(binder.Object),
                Mock.Of<IValueProvider>(),
                new List<IInputFormatter>(),
                new DefaultObjectValidator(new IExcludeTypeValidationFilter[0], metadataProvider),
                Mock.Of<IModelValidatorProvider>());

            // Assert
            Assert.False(result);
            Assert.Null(model.MyProperty);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:27,代码来源:ModelBindingHelperTest.cs


示例2: JsonPatchInputFormatter_ReadsMultipleOperations_Successfully

        public async Task JsonPatchInputFormatter_ReadsMultipleOperations_Successfully()
        {
            // Arrange
            var formatter = new JsonPatchInputFormatter();
            var content = "[{\"op\": \"add\", \"path\" : \"Customer/Name\", \"value\":\"John\"}," +
                "{\"op\": \"remove\", \"path\" : \"Customer/Name\"}]";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var modelState = new ModelStateDictionary();
            var httpContext = GetHttpContext(contentBytes);
            var provider = new EmptyModelMetadataProvider();
            var metadata = provider.GetMetadataForType(typeof(JsonPatchDocument<Customer>));
            var context = new InputFormatterContext(
                httpContext,
                modelName: string.Empty,
                modelState: modelState,
                metadata: metadata);

            // Act
            var result = await formatter.ReadAsync(context);

            // Assert
            Assert.False(result.HasError);
            var patchDoc = Assert.IsType<JsonPatchDocument<Customer>>(result.Model);
            Assert.Equal("add", patchDoc.Operations[0].op);
            Assert.Equal("Customer/Name", patchDoc.Operations[0].path);
            Assert.Equal("John", patchDoc.Operations[0].value);
            Assert.Equal("remove", patchDoc.Operations[1].op);
            Assert.Equal("Customer/Name", patchDoc.Operations[1].path);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:30,代码来源:JsonPatchInputFormatterTest.cs


示例3: ModelType_UsesRuntimeType

        public void ModelType_UsesRuntimeType()
        {
            // Arrange
            var provider = new EmptyModelMetadataProvider();
            var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), new DerivedClass());

            // Act
            var modelType = modelExplorer.ModelType;

            // Assert
            Assert.Equal(typeof(DerivedClass), modelType);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:12,代码来源:ModelExplorerTest.cs


示例4: ModelType_UsesDeclaredType_WhenModelIsNull

        public void ModelType_UsesDeclaredType_WhenModelIsNull()
        {
            // Arrange
            var provider = new EmptyModelMetadataProvider();
            var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model: null);

            // Act
            var modelType = modelExplorer.ModelType;

            // Assert
            Assert.Equal(typeof(BaseClass), modelType);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:12,代码来源:ModelExplorerTest.cs


示例5: GetSimpleDisplayText_WithoutSimpleDisplayProperty

        public void GetSimpleDisplayText_WithoutSimpleDisplayProperty(
            object model, 
            Type modelType, 
            string expectedResult)
        {
            // Arrange
            var provider = new EmptyModelMetadataProvider();
            var modelExplorer = provider.GetModelExplorerForType(modelType, model);

            // Act
            var result = modelExplorer.GetSimpleDisplayText();

            // Assert
            Assert.Equal(expectedResult, result);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:15,代码来源:ModelExplorerExtensionsTest.cs


示例6: ConstructorWithOneParameterInitalizesMembers

        public void ConstructorWithOneParameterInitalizesMembers()
        {
            // Arrange
            var metadataProvider = new EmptyModelMetadataProvider();

            // Act
            var viewData = new ViewDataDictionary(metadataProvider);

            // Assert
            Assert.NotNull(viewData.ModelState);
            Assert.NotNull(viewData.TemplateInfo);
            Assert.Null(viewData.Model);
            Assert.NotNull(viewData.ModelMetadata);
            Assert.Equal(0, viewData.Count);
        }
开发者ID:RehanSaeed,项目名称:Mvc,代码行数:15,代码来源:ViewDataDictionaryTest.cs


示例7: FromStringExpression_SetsContainerAsExpected

        public void FromStringExpression_SetsContainerAsExpected()
        {
            // Arrange
            var myModel = new TestModel { SelectedCategory = new Category() };
            var provider = new EmptyModelMetadataProvider();
            var viewData = new ViewDataDictionary<TestModel>(provider);
            viewData["Object"] = myModel;

            // Act
            var metadata = ExpressionMetadataProvider.FromStringExpression("Object.SelectedCategory",
                                                                           viewData,
                                                                           provider);

            // Assert
            Assert.Same(myModel, metadata.Container.Model);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:16,代码来源:ExpressionMetadataProviderTest.cs


示例8: Constructor_InitalizesMembers

        public void Constructor_InitalizesMembers()
        {
            // Arrange
            var metadataProvider = new EmptyModelMetadataProvider();
            var modelState = new ModelStateDictionary();

            // Act
            var viewData = new ViewDataDictionary<string>(metadataProvider, modelState);

            // Assert
            Assert.Same(modelState, viewData.ModelState);
            Assert.NotNull(viewData.TemplateInfo);
            Assert.Null(viewData.Model);
            Assert.NotNull(viewData.ModelMetadata);
            Assert.Equal(0, viewData.Count);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:16,代码来源:ViewDataDictionaryOfTModelTest.cs


示例9: ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull

        public void ObjectTemplateDisplaysNullDisplayTextWhenObjectIsNull()
        {
            // Arrange
            var html = DefaultTemplatesUtilities.GetHtmlHelper();
            var metadata =
                new EmptyModelMetadataProvider()
                    .GetMetadataForType(null, typeof(DefaultTemplatesUtilities.ObjectTemplateModel));
            metadata.NullDisplayText = "(null value)";
            html.ViewData.ModelMetadata = metadata;

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(metadata.NullDisplayText, result);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:16,代码来源:DefaultDisplayTemplatesTests.cs


示例10: Properties_UsesDeclaredType_WhenModelIsNull

        public void Properties_UsesDeclaredType_WhenModelIsNull()
        {
            // Arrange
            var provider = new EmptyModelMetadataProvider();
            var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), model: null);

            // Act
            var properties = modelExplorer.Properties.ToArray();

            // Assert
            Assert.Equal(1, properties.Length);

            var baseProperty = Assert.Single(properties, p => p.Metadata.PropertyName == "Base1");
            Assert.Equal(typeof(int), baseProperty.Metadata.ModelType);
            Assert.Equal(typeof(BaseClass), baseProperty.Metadata.ContainerType);
            Assert.Same(modelExplorer, baseProperty.Container);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:17,代码来源:ModelExplorerTest.cs


示例11: FromLambaExpression_SetsContainerAsExpected

        public void FromLambaExpression_SetsContainerAsExpected()
        {
            // Arrange
            var myModel = new TestModel { SelectedCategory = new Category() };
            var provider = new EmptyModelMetadataProvider();
            var viewData = new ViewDataDictionary<TestModel>(provider);
            viewData.Model = myModel;

            // Act
            var metadata = ExpressionMetadataProvider.FromLambdaExpression<TestModel, Category>(
                model => model.SelectedCategory,
                viewData,
                provider);

            // Assert
            Assert.Same(myModel, metadata.Container.Model);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:17,代码来源:ExpressionMetadataProviderTest.cs


示例12: JsonFormatterReadsSimpleTypes

        public async Task JsonFormatterReadsSimpleTypes(string content, Type type, object expected)
        {
            // Arrange
            var formatter = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetHttpContext(contentBytes);
            var modelState = new ModelStateDictionary();
            var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, type);
            var context = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            await formatter.ReadAsync(context);

            // Assert
            Assert.Equal(expected, context.Model);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:17,代码来源:JsonInputFormatterTest.cs


示例13: GetBindingContext

        private static ModelBindingContext GetBindingContext(Type modelType)
        {
            var metadataProvider = new EmptyModelMetadataProvider();
            ModelBindingContext bindingContext = new ModelBindingContext
            {
                ModelMetadata = metadataProvider.GetMetadataForType(modelType),
                ModelName = "someName",
                OperationBindingContext = new OperationBindingContext
                {
                    HttpContext = new DefaultHttpContext(),
                    MetadataProvider = metadataProvider,
                }
            };

            bindingContext.OperationBindingContext.HttpContext.Request.Method = "GET";

            return bindingContext;
        }
开发者ID:ryanbrandenburg,项目名称:Mvc,代码行数:18,代码来源:HttpRequestMessageModelBinderTest.cs


示例14: ObjectTemplateDisplaysSimpleDisplayTextWhenTemplateDepthGreaterThanOne

        public void ObjectTemplateDisplaysSimpleDisplayTextWhenTemplateDepthGreaterThanOne()
        {
            // Arrange
            var model = new DefaultTemplatesUtilities.ObjectTemplateModel();
            var html = DefaultTemplatesUtilities.GetHtmlHelper(model);
            var metadata =
                new EmptyModelMetadataProvider()
                    .GetMetadataForType(() => model, typeof(DefaultTemplatesUtilities.ObjectTemplateModel));
            metadata.SimpleDisplayText = "Simple Display Text";
            html.ViewData.ModelMetadata = metadata;
            html.ViewData.TemplateInfo.AddVisited("foo");
            html.ViewData.TemplateInfo.AddVisited("bar");

            // Act
            var result = DefaultDisplayTemplates.ObjectTemplate(html);

            // Assert
            Assert.Equal(metadata.SimpleDisplayText, result);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:19,代码来源:DefaultDisplayTemplatesTests.cs


示例15: JsonFormatterReadsComplexTypes

        public async Task JsonFormatterReadsComplexTypes()
        {
            // Arrange
            var content = "{name: 'Person Name', Age: '30'}";
            var formatter = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes(content);

            var httpContext = GetHttpContext(contentBytes);
            var modelState = new ModelStateDictionary();
            var metadata = new EmptyModelMetadataProvider().GetMetadataForType(null, typeof(User));
            var context = new InputFormatterContext(httpContext, metadata, modelState);

            // Act
            await formatter.ReadAsync(context);

            // Assert
            var model = Assert.IsType<User>(context.Model);
            Assert.Equal("Person Name", model.Name);
            Assert.Equal(30, model.Age);
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:20,代码来源:JsonInputFormatterTest.cs


示例16: CopyConstructors_InitalizeModelAndModelMetadataBasedOnSource

        public void CopyConstructors_InitalizeModelAndModelMetadataBasedOnSource()
        {
            // Arrange
            var metadataProvider = new EmptyModelMetadataProvider();
            var model = new TestModel();
            var source = new ViewDataDictionary<object>(metadataProvider)
            {
                Model = model
            };
            source["foo"] = "bar";
            source.TemplateInfo.HtmlFieldPrefix = "prefix";

            // Act
            var viewData1 = new ViewDataDictionary<object>(source);
            var viewData2 = new ViewDataDictionary(source);

            // Assert
            Assert.NotNull(viewData1.ModelState);
            Assert.NotNull(viewData1.TemplateInfo);
            Assert.Equal("prefix", viewData1.TemplateInfo.HtmlFieldPrefix);
            Assert.NotSame(source.TemplateInfo, viewData1.TemplateInfo);
            Assert.Same(model, viewData1.Model);
            Assert.NotNull(viewData1.ModelMetadata);
            Assert.Equal(typeof(TestModel), viewData1.ModelMetadata.ModelType);
            Assert.Same(source.ModelMetadata, viewData1.ModelMetadata);
            Assert.Equal(source.Count, viewData1.Count);
            Assert.Equal("bar", viewData1["foo"]);
            Assert.IsType<CopyOnWriteDictionary<string, object>>(viewData1.Data);

            Assert.NotNull(viewData2.ModelState);
            Assert.NotNull(viewData2.TemplateInfo);
            Assert.Equal("prefix", viewData2.TemplateInfo.HtmlFieldPrefix);
            Assert.NotSame(source.TemplateInfo, viewData2.TemplateInfo);
            Assert.Same(model, viewData2.Model);
            Assert.NotNull(viewData2.ModelMetadata);
            Assert.Equal(typeof(TestModel), viewData2.ModelMetadata.ModelType);
            Assert.Same(source.ModelMetadata, viewData2.ModelMetadata);
            Assert.Equal(source.Count, viewData2.Count);
            Assert.Equal("bar", viewData2["foo"]);
            Assert.IsType<CopyOnWriteDictionary<string, object>>(viewData2.Data);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:41,代码来源:ViewDataDictionaryOfTModelTest.cs


示例17: MultipartFormatter_CanRead_ReturnsTrueForSupportedMediaTypes

        public void MultipartFormatter_CanRead_ReturnsTrueForSupportedMediaTypes(string requestContentType)
        {
            // Arrange
            var formatter = new MultipartFormatter();
            var httpContext = new DefaultHttpContext();
            httpContext.Request.ContentType = requestContentType;

            var provider = new EmptyModelMetadataProvider();
            var metadata = provider.GetMetadataForType(typeof(void));
            var context = new InputFormatterContext(
                httpContext,
                modelName: string.Empty,
                modelState: new ModelStateDictionary(),
                metadata: metadata);

            // Act
            var result = formatter.CanRead(context);

            // Assert
            Assert.True(result);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:21,代码来源:InputFormatterTest.cs


示例18: CanRead_ReturnsTrueForAnySupportedContentType

        public void CanRead_ReturnsTrueForAnySupportedContentType(string requestContentType, bool expectedCanRead)
        {
            // Arrange
            var formatter = new JsonInputFormatter();
            var contentBytes = Encoding.UTF8.GetBytes("content");

            var httpContext = GetHttpContext(contentBytes, contentType: requestContentType);
            var provider = new EmptyModelMetadataProvider();
            var metadata = provider.GetMetadataForType(typeof(string));
            var formatterContext = new InputFormatterContext(
                httpContext,
                modelName: string.Empty,
                modelState: new ModelStateDictionary(),
                metadata: metadata);

            // Act
            var result = formatter.CanRead(formatterContext);

            // Assert
            Assert.Equal(expectedCanRead, result);
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:21,代码来源:JsonInputFormatterTest.cs


示例19: Properties_UsesRuntimeType

        public void Properties_UsesRuntimeType()
        {
            // Arrange
            var provider = new EmptyModelMetadataProvider();
            var modelExplorer = provider.GetModelExplorerForType(typeof(BaseClass), new DerivedClass());

            // Act
            var properties = modelExplorer.Properties.ToArray();

            // Assert
            Assert.Equal(2, properties.Length);

            var baseProperty = Assert.Single(properties, p => p.Metadata.PropertyName == "Base1");
            Assert.Equal(typeof(int), baseProperty.Metadata.ModelType);
            Assert.Equal(typeof(DerivedClass), baseProperty.Metadata.ContainerType);
            Assert.Same(modelExplorer, baseProperty.Container);

            var derivedProperty = Assert.Single(properties, p => p.Metadata.PropertyName == "Derived1");
            Assert.Equal(typeof(string), derivedProperty.Metadata.ModelType);
            Assert.Equal(typeof(DerivedClass), derivedProperty.Metadata.ContainerType);
            Assert.Same(modelExplorer, derivedProperty.Container);
        }
开发者ID:njannink,项目名称:sonarlint-vs,代码行数:22,代码来源:ModelExplorerTest.cs


示例20: ModelStateConstructorWithDetail_AddsCorrectDictionaryItems

        public void ModelStateConstructorWithDetail_AddsCorrectDictionaryItems()
        {
            // Arrange
            ModelStateDictionary modelState = new ModelStateDictionary();
            var provider = new EmptyModelMetadataProvider();
            var metadata = provider.GetMetadataForProperty(typeof(string), nameof(string.Length));
            modelState.AddModelError("[0].Name", "error1");
            modelState.AddModelError("[0].Name", "error2");
            modelState.AddModelError("[0].Address", "error");
            modelState.AddModelError("[2].Name", new Exception("OH NO"), metadata);

            // Act
            HttpError error = new HttpError(modelState, true);

            // Assert
            HttpError modelStateError = error["ModelState"] as HttpError;

            Assert.Contains(new KeyValuePair<string, object>("Message", "The request is invalid."), error);
            Assert.Contains("error1", modelStateError["[0].Name"] as IEnumerable<string>);
            Assert.Contains("error2", modelStateError["[0].Name"] as IEnumerable<string>);
            Assert.Contains("error", modelStateError["[0].Address"] as IEnumerable<string>);
            Assert.True(modelStateError.ContainsKey("[2].Name"));
            Assert.Contains("OH NO", modelStateError["[2].Name"] as IEnumerable<string>);
        }
开发者ID:phinq19,项目名称:git_example,代码行数:24,代码来源:HttpErrorTest.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ModelBinding.ModelBindingContext类代码示例发布时间:2022-05-26
下一篇:
C# Abstractions.ParameterDescriptor类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap