コード例 #1
0
        public async Task BindModel_WithNestedCollectionContainingARecursiveRelation()
        {
            // Arrange
            Dictionary <string, string> bodyParameters = new Dictionary <string, string>
            {
                { "People[0].Name", "Person 0" },
                { "People[0].Sibling.Name", "Person 0 Sibling" },
                { "People[1].Sibling.Name", "Person 1 Sibling" },
                { "People[2].Sibling", "Person 2 Sibling" },
                { "People[1000].Name", "Person 1000 Sibling" },
            };
            HttpRequestMessage request = new HttpRequestMessage(
                HttpMethod.Post,
                "http://localhost/ModelBinding/NestedCollectionOfRecursiveTypes"
                )
            {
                Content = new FormUrlEncodedContent(bodyParameters)
            };

            // Act
            HttpResponseMessage response = await SubmitRequestAsync(request);

            // Assert
            PeopleModel result = await ReadAsJson <PeopleModel>(response);

            Assert.Equal(3, result.People.Count);
            Person person = result.People[0];

            Assert.Equal("Person 0", person.Name);
            Assert.Equal("Person 0 Sibling", person.Sibling.Name);
            Assert.Null(person.Sibling.Sibling);

            person = result.People[1];
            Assert.Equal("Person 1 Sibling", person.Sibling.Name);
            Assert.Null(person.Sibling.Sibling);

            person = result.People[2];
            Assert.Null(person.Name);
            Assert.NotNull(person.Sibling);
            Assert.Null(person.Sibling.Name);
        }
コード例 #2
0
        public async Task BindModel_WithNestedCollectionContainingRecursiveRelation_WithMalformedValue()
        {
            // Arrange
            Dictionary <string, string> bodyParameters = new Dictionary <string, string>
            {
                { "People", "Person 0" },
            };
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,
                                                                "http://localhost/ModelBinding/NestedCollectionOfRecursiveTypes")
            {
                Content = new FormUrlEncodedContent(bodyParameters)
            };

            // Act
            HttpResponseMessage response = await SubmitRequestAsync(request);

            // Assert
            PeopleModel result = await ReadAsJson <PeopleModel>(response);

            Person person = Assert.Single(result.People);

            Assert.Null(person.Name);
            Assert.Null(person.Sibling);
        }
コード例 #3
0
 public PeopleModel NestedCollectionType([FromBody] PeopleModel model)
 {
     return(model);
 }