API with ASP.NET, running a POST sends blanks

0

Hi, I'm pretty new to this .net and had to create an API to make a simple record, create it using the sig. video link

The API works I can do the GET, POST, PUT but note that when I do a POST the data is saved with a lot of blank spaces, example my mail field in the Microsoft SQL Server database only allows 50 spaces then if you send

"andres"

It will be registered

"andres (34 blank spaces)"

With all the missing blanks I want to suppose that as it takes the model of the database it sends like this, as I said a lot about .net so if you can help me I would appreciate it

This is the model

    namespace ApiV2.Models
    {
        using System;
        using System.Data.Entity;
        using System.ComponentModel.DataAnnotations.Schema;
        using System.Linq;

        public partial class Modelo : DbContext
        {
            public Modelo()
                : base("name=Modelo1")
            {
            }

            public virtual DbSet<usuarios> usuarios { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<usuarios>()
                    .Property(e => e.u_email)
                    .IsFixedLength();

                modelBuilder.Entity<usuarios>()
                    .Property(e => e.u_nombre)
                    .IsFixedLength();

                modelBuilder.Entity<usuarios>()
                    .Property(e => e.u_password)
                    .IsFixedLength();
            }
        }
    }

Y este el controlador

namespace ApiV2.Controllers
{
    public class usuariosController : ApiController
    {
        private Modelo db = new Modelo();

        // GET: api/usuarios
        public IQueryable<usuarios> Getusuarios()
        {
            return db.usuarios;
        }

        // GET: api/usuarios/5
        [ResponseType(typeof(usuarios))]
        public async Task<IHttpActionResult> Getusuarios(int id)
        {
            usuarios usuarios = await db.usuarios.FindAsync(id);
            if (usuarios == null)
            {
                return NotFound();
            }

            return Ok(usuarios);
        }

        // PUT: api/usuarios/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> Putusuarios(int id, usuarios usuarios)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != usuarios.u_id)
            {
                return BadRequest();
            }

            db.Entry(usuarios).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!usuariosExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/usuarios
        [ResponseType(typeof(usuarios))]
        public async Task<IHttpActionResult> Postusuarios(usuarios usuarios)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.usuarios.Add(usuarios);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (usuariosExists(usuarios.u_id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = usuarios.u_id }, usuarios);
        }

        // DELETE: api/usuarios/5
        [ResponseType(typeof(usuarios))]
        public async Task<IHttpActionResult> Deleteusuarios(int id)
        {
            usuarios usuarios = await db.usuarios.FindAsync(id);
            if (usuarios == null)
            {
                return NotFound();
            }

            db.usuarios.Remove(usuarios);
            await db.SaveChangesAsync();

            return Ok(usuarios);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool usuariosExists(int id)
        {
            return db.usuarios.Count(e => e.u_id == id) > 0;
        }
    }
}

I do not know if they need more info from the base, they are only Varchar fields they do not have anything in specific.

    
asked by rances5171 28.09.2018 в 01:16
source

1 answer

1

rances, when using in your model, .IsFixedLength(); you indicate that it must have a fixed length, in such case you fill it with blank spaces.

If you do not want to use that behavior, just quilato.

    
answered by 28.09.2018 / 01:36
source