2014-12-07 92 views
0

我试图将新添加的实体作为批量请求发布到我的odata v3 web api使用微风,但应该传递给我的odata post方法的实体始终为空。批处理请求实体始终为空

我的批次路由配置:

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

我的控制器交方法,该方法是具有空entitiy recieving呼叫:

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(); 

当我检查与小提琴手请求我看到它发送正确的数据我加入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上生成元数据。

+0

向我们展示breezejs代码和实体? – 2014-12-08 07:35:09

+0

用我的实体和微风代码更新了问题,谢谢。 – 2014-12-08 11:56:22

+0

什么是ApiUserRole和ApiPermission?他们看起来很类,但是你将它们用作整数值。 – 2014-12-08 11:58:26

回答

0

我找到了解决方法。我的控制器来自Odata控制器。一旦我将其更改为Apicontroller,我的批处理请求就起作用了!不知道为什么。也可能会丢失odata控制器提供的许多功能。因此需要进一步调试才能找到真正的原因。