Reputation: 1934
I am having issues reading the data in my table using a Web API, so to keep it simple I have a local SQL Server instance and have a simple table called Gender
.
This how my model:
Gender.Model
:
public class Gender
{
public int Id { get; set; }
public string Description { get; set; }
//public Language LanguageId { get; set; }
public int InternalCode { get; set; }
public bool isActive { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
I also use a DbContext
class as follows:
...
using System.Data.Entity;
namespace API.Models
{
public class APIContext : DbContext
{
public DbSet<Gender> Genders { get; set; }
}
}
I also configured a connection string in my web.config
:
<connectionStrings>
<add name="APIContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>
</connectionStrings>
Here is my gender controller
namespace API.Controllers
{
public class GendersController : ApiController
{
private APIContext db = new APIContext();
// Index
// URI GET + api/Genders
[HttpGet]
public HttpResponseMessage Genders()
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, db.Genders.ToList());
return response;
}
}
}
When I go in fiddler and compose the following http://localhost:52024/api/Genders
I however receive a 200 http code but my array is blank I received this raw message
[]
Is there a step that I miss?
Upvotes: 1
Views: 118
Reputation: 317
can you try this one
Ilist<Genders> genders = null;
genders = db.Genders;
HttpResponseMessage response = Request.CreateResponse<Ilist<Genders>>(HttpStatusCode.OK, genders);
return response;
Upvotes: 4