简体   繁体   中英

C# Odata WebApi List of Elements in Post

i could realy need some help.

Im coding a small WebApi Program.

While Unit testing i noticed i cant send a list of Elements to my post Method.

First take a look at my Unit Test:

 [TestMethod]
    public async Task PostWithExpandoObjectSerilasationToDataType()
    {
        //Define an imaginary Car Object
        List<dynamic> listExpando = new List<dynamic>();

        dynamic obj1 = new ExpandoObject();
        obj1.Attribute = "PS";
        obj1.DataType = "Integer";
        obj1.EntityId = "3";
        obj1.DefaultValue = "";

        dynamic obj2 = new ExpandoObject();
        obj2.Attribute = "Color";
        obj2.DataType = "Text Only";
        obj2.EntityId = "3";
        obj2.DefaultValue = "";

        dynamic obj3 = new ExpandoObject();
        obj3.Attribute = "Km";
        obj3.DataType = "Number";
        obj3.EntityId = "3";
        obj3.DefaultValue = "1.3";

        listExpando.Add(obj1);
        listExpando.Add(obj2);
        listExpando.Add(obj3);

        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
        javaScriptSerializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJsonconverter() });

        string jsonOfTest = javaScriptSerializer.Serialize(listExpando);

        // arrange
        UnityContainer container = UnityConfig.RegisterComponents();
        var start = new Startup(container);

        using (TestServer server = TestServer.Create(builder => start.Configuration(builder)))
        {
            using (var client = new HttpClient(server.Handler))
            {

                HttpContent content = new StringContent(jsonOfTest, Encoding.UTF8, "application/json");

                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://testserver/odata/Attributes"),
                    Method = HttpMethod.Post,
                    Content = content
                };

                string jsonContent = content.ReadAsStringAsync().Result;
               request.Headers.Add("Prefer", "return=representation");

                // act
                HttpResponseMessage response = await client.SendAsync(request);
                Attributes result = response.Content.ReadAsAsync<Attributes>().Result;

                // assert
                Assert.IsNotNull(response);
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
                Assert.IsNotNull(result, "No result content found");
                Assert.IsNotNull(result.Attribute);
            }
        }
    }

Next here is my Post Method:

 public IHttpActionResult Post([ModelBinder]IEnumerable<Attributes> att)
    {....}

When it comes to the Post Method att is alway null

When trying the Service with Rest i get this message:

"message": "Collection(EAVService.Entities.Attributes) is not an entity type. Only entity types are supported."

Googled and tried different things but i didnt found a working solution

Any one there who can help me to solve my problem? :)

Best Regards Andre

In OData, POST is normally used to add a single entity to a collection . So your Post method should have the following signature:

public IHttpActionResult Post(Attributes att)

See "Adding an Entity to the Entity Set" in Create an OData v4 Endpoint Using ASP.NET Web API 2.2 for a complete example.

If you need to create multiple Attributes entities in a single service call, you have two options:

  • Implement batch request handling in your service

  • Implement an OData action that accepts a collection of Attributes entities and adds them in bulk to the appropriate entity set

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM