ASP.NET Web API post Byte[]

Asked

Viewed 210 times

0

I’m creating a Web API and my model has a property byte[], but every time I try to make a Post my model arrives null, taking away the property byte[] he usually arrives 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; }
}

the 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 the Sdk of Azure to work with data synchronization in an application mobile.

But still the question remains, how to give a Post in a model with a type property byte[]?

1 answer

2


But still the question remains, how to give a Post in a model with a type property byte[]?

I wanted to understand why you had one Timestamp in client, and it is not even a property that should be visible to the user.

Anyway, to send one byte[] for an API, you need to use a Media Formatter what support byte[], as BSON (Binary JSON):

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

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

  • 1

    So, what I’m trying to create is a backend to work with data offline on a mobile client, now I’ve read the documentation better and found an example of Azure itself that shows better how to build this backend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.