GET method returning empty

Asked

Viewed 99 times

0

Good evening, I am developing a Webapi, I am using the code-first approach, for this I have selected the Entity framework 6.0.1, in my project I have the following classes Personcontroller.Cs, Configurations.Cs (where I enter the data manually for now)and my Person class, I am using Postman to simulate the request, I looked for some examples here on the site but no equivalent to mine, in case someone can help me thank.

Personcontroller.Cs

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Models;
using WebApi.Contexto;

namespace WebApi.Controllers
{
    public class PersonController : ApiController
    {
        private readonly Context contexto = new Context();
        [Route("api/person")]
        [HttpGet]
        public IHttpActionResult getAll(string search)
        {
            if (string.IsNullOrEmpty(search))
            {
                search = "";
            }
            var list = contexto.People.Where(x => x.firstName.Contains(search) || x.lastName.Contains(search));

            return Ok(list);

        } 
     }
}

Configurations.Cs

namespace WebApi.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WebApi.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<WebApi.Contexto.Context>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(WebApi.Contexto.Context context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.

            context.People.Add(new Person { firstName = "Andrew ", lastName = "Teste", id = 1, birthDate = new DateTime(2021, 01, 31) });
           
        }
    }
}
 

Person.Cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApi.Models
{
    public class Person
    {
        public int id { get; set; }
        public string firstName { get; set; }
        public string lastName { get; set; }
        public DateTime birthDate { get; set; }
    }
}
  • What tests were done? What was the expected result in each case?

  • I expected to return a JSON in Postman contenting the information of the last line of my Configuration class

  • And what requests were made and what values should be presented?

  • GET has only been performed so far, but instead of returning an array with the information in a JSON format it only returns me " [ ] "

  • And what you’re going through in the parameter search?

1 answer

0


I managed to find the solution to the error, I will leave here in case someone goes through the same. In my connection string I forgot the parameter:

Integrated security = True

With this it was not possible to connect to the database, since I am using Windows Authenticate.

After this correction is only run this command:

update-database

Browser other questions tagged

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