ASP.NET Core WebApi with SQL Server and EFCore 2

.net-core asp.net-core-webapi c# entity-framework-core

Question

I'm trying to create my first .NET Core Web Api with SQL Server and Entity Framework Core 2, but when I call the endpoint from my browser while project build, I get nothing. It always stays at the startup page that says "Hello World".

How to get data from the Rest Api correctly?

First, my model class:

public class Project
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

With a DbContext:

public class TaskManagerDbContext : DbContext
{
    public TaskManagerDbContext(DbContextOptions<TaskManagerDbContext> options) : base(options) { }

    public DbSet<Project> Projects { get; set; }
}

Then I hardcoded two records into database:

enter image description here

I generated ProjectsController by scaffolding an API with R/W based on Entity Framework. The get function look like this (I think there is no sense to show all controller because its generated by the scaffold):

[Route("api/[controller]")]
[ApiController]
public class ProjectsController : ControllerBase
{
    private readonly TaskManagerDbContext _context;

    public ProjectsController(TaskManagerDbContext context)
    {
        _context = context;
    }

    // GET: api/Projects
    [HttpGet]
    public IEnumerable<Project> GetProjects()
    {
        return _context.Projects;
    }

    // GET: api/Projects/5
    [HttpGet("{id}")]
    public async Task<IActionResult> GetProject([FromRoute] int id)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var project = await _context.Projects.FindAsync(id);

        if (project == null)
        {
            return NotFound();
        }

        return Ok(project);
    }
}

Finally, my Startup.cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddDbContext<TaskManagerDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });

        app.UseHttpsRedirection();
        app.UseMvc();
    }
}

Question is, what change is needed to get data from my database by API endpoints? For now, when I try to call the endpoint, it always stays on the "hello world" page.

enter image description here

My connection string is in appsettings.json file in project directory.

Thanks for any advice!

1
0
12/13/2018 8:41:10 PM

Accepted Answer

You've got some remnant left from the template for an empty asp core project.

Just remove this entire block, because you don't need it:

app.Run(async (context) =>
{
    await context.Response.WriteAsync("Hello World!");
});
4
12/13/2018 8:13:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow