Search code examples
c#asp.net-web-apiodatabreeze

Batch request entities always null


I am trying to post newly added entities as a batch request to my odata v3 web api using breeze but the entities that should be getting passed to my odata post methods are always null.

My batch route configuration:

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

My controller post method that is recieving the call with an empty entitiy:

public IHttpActionResult Post([FromBody]ApiUserEntity apiUserEntity)
{
    if (apiUserEntity == null)
       return BadRequest(ModelState);
}

The entity:

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
}

Simplified code of how i call the savechanges with breeze

manager.createEntity('ApiUserEntity',
                                   { 
                                       Id: 1,
                                       Username: "user",
                                       Password: "password",
                                       Email: "Email", 
                                       Salt: "Salt",
                                       Role: "1",
                                       Permission: "1"
                                    });
manager.saveChanges();

When i inspect the request with fiddler i see that it is sending the correct data i added with 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--

And the post method on the controller is being hit when i debug, but the entity is always null. I am using entity framework and have generated the metadata on the webapi using the conventionmodelbuilder.


Solution

  • I found a workaround. My controllers were deriving from Odata controller. Once i changed it to Apicontroller my batch requests worked! Not sure why though. Also probably losing alot of the functionality that the odata controller provided. So will need to debug some more to find the actual reason.