【发布时间】:2014-12-12 06:12:06
【问题描述】:
我正在尝试使用微风将新添加的实体作为批处理请求发布到我的 odata v3 Web api,但应该传递给我的 odata 发布方法的实体始终为空。
我的批量路由配置:
config.Routes.MapODataServiceRoute(
routeName: "odata",
routePrefix: "odata",
model: builder.GetEdmModel(),
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)
);
我的控制器 post 方法正在接收带有空实体的调用:
public IHttpActionResult Post([FromBody]ApiUserEntity apiUserEntity)
{
if (apiUserEntity == null)
return BadRequest(ModelState);
}
实体:
public class BaseEntity
{
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
}
public class ApiUserEntity : BaseEntity
{
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Salt { get; set; }
public ApiUserRole Role { get; set; }
public ApiPermission Permission { get; set; }
}
enum ApiUserRole {
Admin,
Staff,
User
}
enum ApiPermission {
Read,
Write,
ReadWrite
}
我如何使用微风调用 savechanges 的简化代码
manager.createEntity('ApiUserEntity',
{
Id: 1,
Username: "user",
Password: "password",
Email: "Email",
Salt: "Salt",
Role: "1",
Permission: "1"
});
manager.saveChanges();
当我用 fiddler 检查请求时,我发现它正在发送我用breezejs 添加的正确数据:
POST http://localhost:22594/odata/$batch HTTP/1.1
Host: localhost:22594
Connection: keep-alive
Content-Length: 640
Pragma: no-cache
Cache-Control: no-cache
MaxDataServiceVersion: 3.0
Origin: http://localhost:51406
User-Agent: Mozilla/ 5.0 (Windows NT 6.3; WOW64) AppleWebKit/ 537.36(KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
Content-Type: multipart/mixed; boundary = batch_fffa - 6088 - 92e7
Accept: multipart/mixed
DataServiceVersion: 2.0
Referer: http://localhost:51406/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en; q=0.8,nl; q=0.6,nb; q=0.4,es; q = 0.2
--batch_fffa - 6088 - 92e7
Content-Type: multipart / mixed; boundary = changeset_d571 - 5fc6 - 6f89
--changeset_d571 - 5fc6 - 6f89
Content-Type: application / http
Content - Transfer-Encoding: binary
POST ApiUsers HTTP / 1.1
Content-ID: 1
DataServiceVersion: 2.0
Accept: application / atomsvc + xml; q = 0.8, application / json; odata = fullmetadata; q = 0.7, application / json; q = 0.5, */*;q=0.1
Content-Type: application/json
MaxDataServiceVersion: 3.0
{"Username":"name","Password":"password","Email":"email","Salt":"dasdasdasd","Role":"1","Permission":"1","Id":1,"CreatedAt":"1899-12-31T23:00:00"}
--changeset_d571-5fc6-6f89--
--batch_fffa-6088-92e7--
当我调试时,控制器上的 post 方法被命中,但实体始终为空。我正在使用实体框架,并使用conventionmodelbuilder在webapi上生成了元数据。
【问题讨论】:
-
向我们展示breezejs 代码和您的实体?
-
用我的实体和微风代码更新了问题,谢谢。
-
什么是 ApiUserRole 和 ApiPermission?它们看起来像类,但您将它们用作整数值。
-
这些是枚举,我也将它们添加到问题中。
-
您能否在不为这 2 个枚举指定值的情况下尝试您的请求?
标签: c# asp.net-web-api odata breeze