Search code examples
c#.net-coreasp.net-core-webapiminimal-apisasp.net-core-8

Problems with Microsoft Endpoint Explorer Generated .http File


I am following an MS Tutorial that creates a minimal API in ASP.NET Core 8.0 and uses Endpoint Explorer in Visual Studio 2022 to generate an .http file to test the API endpoints.

Code for the model:

public class Todo
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public bool IsComplete { get; set; }
}

Code for database context:

using Microsoft.EntityFrameworkCore;

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

    public DbSet<Todo> Todos => Set<Todo>();
}

Code for API:

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

var app = builder.Build();

app.MapGet("/todoitems", async (TodoDb db) =>
        await db.Todos.ToListAsync());

app.MapGet("/todoitems/complete", async (TodoDb db) =>
        await db.Todos.Where(t => t.IsComplete).ToListAsync());

app.MapGet("/todoitems/{id}", async (int id, TodoDb db) =>
        await db.Todos.FindAsync(id)
            is Todo todo
                ? Results.Ok(todo)
                : Results.NotFound());

app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
    {
        db.Todos.Add(todo);
        await db.SaveChangesAsync();

        return Results.Created($"/todoitems/{todo.Id}", todo);
    });

app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) =>
    {
        var todo = await db.Todos.FindAsync(id);

        if (todo is null) return Results.NotFound();

        todo.Name = inputTodo.Name;
        todo.IsComplete = inputTodo.IsComplete;

        await db.SaveChangesAsync();

        return Results.NoContent();
    });

app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) =>
    {
        if (await db.Todos.FindAsync(id) is Todo todo)
        {
            db.Todos.Remove(todo);
            await db.SaveChangesAsync();
            return Results.NoContent();
        }

        return Results.NotFound();
    });

app.Run();

We are testing the posting data part here:

app.MapPost("/todoitems", async (Todo todo, TodoDb db) =>
{
   db.Todos.Add(todo);
   await db.SaveChangesAsync();

   return Results.Created($"/todoitems/{todo.Id}", todo);
 });

Then the tutorial instructs to use the Endpoints Explorer in VS2022 to generate a request (.http file), which I did.

enter image description here

Here's the code in the .http file:

@TodoApi_HostAddress = https://localhost:7284

Post {{TodoApi_HostAddress}}/todoitems
Content-Type: application/json

{
  "name":"walk dog",
  "isComplete":true
}

###

The problem I am having is that when I run the project click the Send Request link in the .http file, I am getting an error "Last request had errors. Click to send a new request." and no Response Pane appears in VS.

enter image description here

Error:

enter image description here

However, when I test this using Postman, I have no problem:

enter image description here

Any idea of what I've done wrong?

Thanks in advance and Merry Christmas


Solution

  • After reboot problem was resolved.

    Thanks all