ASP.NET Web API post byte []

0

I'm creating a Web API and my model has a byte[] property, however whenever I try to Post my model arrives null, taking the byte[] property it normally arrives at the model .

[Table("Pessoas")]
public class Pessoa : ITableData
{
    public string Id { get; set; }
    public string Nome { get; set; }
    public DateTime DataNascimento { get; set; }
    public byte[] Version { get; set; }
    public DateTimeOffset? CreatedAt { get; set; }
    public DateTimeOffset? UpdatedAt { get; set; }
    public bool Deleted { get; set; }
}

property byte[] Version is an implementation of ITableData .

// POST: api/Pessoas
[HttpPost]
public async Task<IActionResult> PostPessoa([FromBody] Pessoa pessoa)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    _context.Pessoas.Add(pessoa);
    await _context.SaveChangesAsync();

    return CreatedAtAction("GetPessoa", new { id = pessoa.Id }, pessoa);
}

The implementation of ITableData is part of a future attempt to work with Sdk Azure to work with data synchronization in a mobile application.

But still the question is, how do you give a Post in a model with a property of type byte[] ?

    
asked by anonymous 13.05.2017 / 22:00

1 answer

2
  

But still the question is, how do you give a Post in a model with a property of type byte[] ?

I wanted to understand why you have Timestamp in the client, which is not even a property that should be visible to the user.

Anyway, to send byte[] to an API, you need to use a Media Formatter that supports byte[] , such as BSON (Binary JSON):

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.Add(new BsonMediaTypeFormatter());
    }
}

But this information will have to be serialized in BSON to be understood correctly. For this, you will have to use something that serialize on BSON on the client, something like js-bson .

    
13.05.2017 / 22:36