批处理请求实体始终为null

时间:2014-12-07 23:32:16

标签: c# asp.net-web-api odata breeze

我正在尝试使用breeze将新添加的实体作为批处理请求发布到我的odata v3 web api,但应该传递给我的odata post方法的实体始终为null。

我的批量路线配置:

config.Routes.MapODataServiceRoute(
    routeName: "odata",
    routePrefix: "odata",
    model: builder.GetEdmModel(),
    batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)
 );

我的控制器发布方法接收带有空权限的呼叫:

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
}

我如何使用breeze调用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方法被击中,但实体始终为null。我正在使用实体框架,并使用conventionmodelbuilder在webapi上生成元数据。

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。我的控制器来自Odata控制器。一旦我将其更改为Apicontroller,我的批处理请求就可以了!不知道为什么。也可能失去了odata控制器提供的大量功能。因此需要调试一些以找到实际原因。