Post 中的 C# Odata WebApi 元素列表
C# Odata WebApi List of Elements in Post
我真的需要一些帮助。
我正在编写一个小型 WebApi 程序。
在进行单元测试时,我注意到我无法将元素列表发送到我的 post 方法。
先看看我的单元测试:
[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);
}
}
}
接下来是我的Post方法:
public IHttpActionResult Post([ModelBinder]IEnumerable<Attributes> att)
{....}
说到Post方法
att 始终为 null
尝试使用 Rest 服务时,我收到此消息:
"message": "Collection(EAVService.Entities.Attributes) is not an entity type. Only entity types are supported."
用谷歌搜索并尝试了不同的方法,但我没有找到有效的解决方案
有谁能帮我解决我的问题吗? :)
最好的问候
安德烈
在OData中,POST通常用于add a single entity to a collection。因此,您的 Post
方法应具有以下签名:
public IHttpActionResult Post(Attributes att)
有关完整示例,请参阅 Create an OData v4 Endpoint Using ASP.NET Web API 2.2 中的 "Adding an Entity to the Entity Set"。
如果您需要在单个服务调用中创建多个 Attributes
实体,您有两种选择:
在您的服务中实施 batch request 处理
实施一个 OData action 接受 Attributes
实体的集合并将它们批量添加到适当的实体集
我真的需要一些帮助。
我正在编写一个小型 WebApi 程序。
在进行单元测试时,我注意到我无法将元素列表发送到我的 post 方法。
先看看我的单元测试:
[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);
}
}
}
接下来是我的Post方法:
public IHttpActionResult Post([ModelBinder]IEnumerable<Attributes> att)
{....}
说到Post方法 att 始终为 null
尝试使用 Rest 服务时,我收到此消息:
"message": "Collection(EAVService.Entities.Attributes) is not an entity type. Only entity types are supported."
用谷歌搜索并尝试了不同的方法,但我没有找到有效的解决方案
有谁能帮我解决我的问题吗? :)
最好的问候 安德烈
在OData中,POST通常用于add a single entity to a collection。因此,您的 Post
方法应具有以下签名:
public IHttpActionResult Post(Attributes att)
有关完整示例,请参阅 Create an OData v4 Endpoint Using ASP.NET Web API 2.2 中的 "Adding an Entity to the Entity Set"。
如果您需要在单个服务调用中创建多个 Attributes
实体,您有两种选择:
在您的服务中实施 batch request 处理
实施一个 OData action 接受
Attributes
实体的集合并将它们批量添加到适当的实体集